I'm starting a Python script that I've created using start-stop-daemon
. It works perfectly in all cases, except when I've forgotten that I've already started it. It creates a new process, but is unable to kill the old processes automatically.
Here's the complete script:
PYTHONPATH=/usr/lib/python2.4
# path to app
APP_PATH=/var/spool/EARS
# path to paster bin
DAEMON=/var/spool/EARS/pymilter_test8.py
# startup args
#DAEMON_OPTS=" serve --log-file <my logfile> --server-name=main production.ini"
# script name
NAME=EARS_milter.sh
# app name
DESC='EARS_milter'
# pylons user
RUN_AS=postfix
PID_FILE=/var/run/milter.pid
############### END EDIT ME ##################
test -x $DAEMON || exit 0
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --pidfile $PID_FILE
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --pidfile $PID_FILE
sleep 1
start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
Is there a way to have it check? I've tried the -S switch with no luck so far.
Thanks.
$PID_FILE
will contain the process id only of the latest instance you started, so only this instance will be killed.Something you could do to resolve this, is to grep the PIDs of the running instances of your daemon and terminate them using
kill
....or just check if there's another running instance of your script, before calling
start-stop-daemon
.What is being spawned and ran is the Python interpreter, but
start-stop-daemon
checks for a process namedpymilter_test8.py
and obviously can't find it.I had an issue recently when I was starting a database server (Crate) using
start-stop-daemon
. The database was started with abash
script that then startedjava
in a separate process. This was a problem because the actual server was always listed under the second PID. When I made the server itself create the PID file, I found thatstart-stop-daemon
would still not recognize that the server was already running when I used the--pidfile
argument. However, when I used--startas
instead of--exec
for the starting call, it seemed to resolve things for me andstart-stop-daemon
was able to manage the server process properly again.You have to make sure that at the least, the PID file is correct as a forking server like I had would use a different running PID than that of the process that starts it. Whilst you're debugging, using the
-t
flag is helpful as you can manually run thestart-stop-daemon
commands in each case and monitor what it would do.You could use a lock file in
/var/lock
. Have the daemon check for a lock file before it starts, and fail with an error if the lock file exists. If it didn't fail, it would then create the lock file. That way two instances couldn't be running at the same time.The only problem would be that if it crashes without having the ability to clean up its mess, you'd have to manually delete the lock file. Not a huge problem. The lock file location should be in the error message created when it fails to start.
In the start case, you'd put something like this:
And then in the stop case, something like this:
It involves editing beyond the normal config editing section, but it should work.