wcs/wcs/ctl/stop.py

64 lines
1.9 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2013 Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import errno
import os
import signal
import sys
import time
from qommon.ctl import Command, make_option
class CmdStop(Command):
name = 'stop'
def __init__(self):
Command.__init__(self, [
make_option('--pidfile', metavar='FILENAME',
dest='pidfile')
])
def execute(self, base_options, sub_options, args):
self.pidfile = sub_options.pidfile
if not self.pidfile:
print >> sys.stderr, 'you must specificy --pidfile'
sys.exit(1)
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
print >> sys.stderr, 'pidfile does not exist. Daemon not running?'
sys.exit(1)
# Try killing the daemon process...
try:
while 1:
os.kill(pid, signal.SIGTERM)
time.sleep(0.1)
except OSError, err:
if err.errno == errno.ESRCH:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print str(err)
sys.exit(1)
CmdStop.register()