Start-stop-daemon won’t start the Python script as service

start-stop-daemonsysvinit

I am trying to run Google AppEngine on my Debian machine, I created a file init.d/gae:

. /lib/lsb/init-functions

#
# Initialize variables
#

name=gae
user=$name

pid=/var/run/$name.pid
prog="python /opt/google_appengine/dev_appserver.py --host=0.0.0.0 --admin_host=0.0.0.0 --php_executable_path=/usr/bin/php-cgi /var/www"


case "${1}" in
   start)
      echo "Starting...Google App Engine"
      start-stop-daemon --start --make-pidfile --background --oknodo --user "$user" --name "$name" --pidfile "$pid" --startas "$prog" 

      ;;

   stop)
      echo "Stopping...Google App Engine"

      ;;

   restart)
      ${0} stop
      sleep 1
      ${0} start
      ;;

   *)
      echo "Usage: ${0} {start|stop|restart}"
      exit 1
      ;;
esac

exit 0

# End scriptname

I am testing the script by manually invoking, and the script runs but not as a daemon or at least it doesn't detach from terminal. I am expecting/looking for similar functionality to Apache.

What switch am I missing?


EDIT

I should note that no PID file is being written or created despite the switch indicating it should be created

Best Answer

You have two problems I can see:

prog=python /opt/google_appengine/dev_appserver.py --host=0.0.0.0 --admin_host=0.0.0.0 --php_executable_path=/usr/bin/php-cgi /var/www

Will start /opt/google_appengine/dev_appserver.py with prog=python in the environment. This is before your start block, so start-stop-daemon isn't even getting involved.

The quick fix is to quote the entire assignment like this:

prog='python /opt/google_appengine/dev_appserver.py --host=0.0.0.0 --admin_host=0.0.0.0 --php_executable_path=/usr/bin/php-cgi /var/www'

But a better fix is to use the style from /etc/init.d/skeleton, and do

DAEMON='python /opt/google/appengine/dev_appserver.py'
DAEMON_ARGS='--host=0.0.0.0 --admin_host=0.0.0.0 --php_executable_path=/usr/bin/php-cgi /var/www'

The second problem is that you're wrongly quoting $prog.

start-stop-daemon --start --make-pidfile --background --oknodo --user "$user" --name "$name" --pidfile "$pid" --startas "$prog"

tells start-stop-daemon to try to start a program called python /opt/google_appengine/dev_appserver.py --host=0.0.0.0 --admin_host=0.0.0.0 --php_executable_path=/usr/bin/php-cgi /var/www.

But clearly there is no program called that. You want to start python with arguments. Removing the double quotes there is the quick fix, but a better one, again following /etc/init.d/skeleton, would be

start-stop-daemon --start --quiet --chuid $CHUID --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS

Related Question