Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Can you post the complete command? –  Khaled Dec 5 '11 at 20:38
    
done. i'm thinking of adding in error-checking outside of start-stop-daemon, but it'd be great if it knew on its own. –  Larry G. Wapnitsky Dec 5 '11 at 20:55
    
I wound up adding a bit of code to the script that checks for running processes with pymilter_test8.py (or whatever I rename it to eventually) via ps and grep prior to starting the daemon. If the processes exist, the service does not attempt to start. –  Larry G. Wapnitsky Dec 6 '11 at 12:42
add comment

1 Answer

$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 named pymilter_test8.py and obviously can't find it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.