Skip to content

Commit

Permalink
[core] improve detection of already running agent
Browse files Browse the repository at this point in the history
And thus add the ability to be restarted properly after a SIGKILL (since
the pid in the pidfile is now tested)
  • Loading branch information
degemer committed Mar 16, 2015
1 parent 950918d commit 741de61
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import errno
import signal

# 3p
from psutil import pid_exists

log = logging.getLogger(__name__)

class AgentSupervisor(object):
Expand Down Expand Up @@ -158,10 +161,14 @@ def start(self, foreground=False):
pid = self.pid()

if pid:
message = "pidfile %s already exists. Is it already running?\n"
log.error(message % self.pidfile)
sys.stderr.write(message % self.pidfile)
sys.exit(1)
# Check if the pid in the pidfile corresponds to a running process
if pid_exists(pid):
log.error("Not starting, another instance is already running"\
" (using pidfile {0})".format(self.pidfile))
sys.exit(1)
else:
log.warn('pidfile contains the pid of a stopped process.'\
' Starting normally')

log.info("Pidfile: %s" % self.pidfile)
if not foreground:
Expand Down

0 comments on commit 741de61

Please sign in to comment.