Bash – start-stop-daemon works at command line but doesn’t work in /etc/init.d script

bashbash-scriptinginit

I'm trying to get a starter script (for a ruby gem called ar_sendmail) working in /etc/init.d/ar_sendmail:

#! /bin/sh

echo "in /etc/init.d/ar_sendmail"
DIR=/home/max/work/e_learning_resource/trunk
PATH=/var/lib/gems/1.8/bin
DAEMON=/var/lib/gems/1.8/bin/ar_sendmail
DAEMON_OPTS="-e production -d --batch-size 100 --delay 150"
NAME=ar_sendmail
DESC=ar_sendmail
PID_FILE=/home/max/work/e_learning_resource/trunk/shared/log/ar_sendmail.pid


test -x $DAEMON || exit 0
set -e

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon -d $DIR --start --quiet --pidfile $PID_FILE \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        kill -TERM `cat $PID_FILE`        
    rm $PID_FILE
        echo "$NAME."
        ;;
  restart)
        echo -n "Restarting $DESC: "
        kill -TERM `cat $PID_FILE`        
    rm $PID_FILE
        sleep 1
        start-stop-daemon -d $DIR --start --quiet --pidfile \
                $PID_FILE --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|reload}" >&2
            exit 1   
            ;;
    esac

    exit 0

It's blowing up on the start-stop-daemon line, saying "start-stop-daemon: not found". But, when i plug the values into that line manually, and run it on the command line, it works.

My first thought was it was the shebang line but #! /bin/sh should be right shouldn't it? It's definitely the right folder and what i use in my other /etc/init.d scripts.

My second thought was that it's sudo related: i'd been testing start-stop-daemon in non-sudo and running /etc/init.d/ar_sendmail in sudo mode. But, i can run start-stop-daemon fine with sudo as well.

Kind of stumped, any ideas?

Best Answer

PATH=/var/lib/gems/1.8/bin

There's your problem.

You're clobbering your PATH variable so it can't find start-stop-daemon in /sbin

Try using:

PATH=${PATH}:/var/lib/gems/1.8/bin

Or something similar instead.

Related Question