Search code examples
node.jsserviceforevercdinit.d

"can't cd" within init.d when started as service


I have a very simple script running in init.d to start a node server using forever. It has been working perfectly until recently and i cannot find what can have changed. I am on Debian 7, have not done any update recently. This is my script (copied from somewhere, and kept simple simple, it is for tests )

#!/bin/sh
#/etc/init.d/nodeup
. /lib/lsb/init-functions
export PATH=$PATH:/usr/local/bin
export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules
. /home/pat/env
cd /var/www/mydir
FOREVER_ROOT=/var/log/forever/mydir
export FOREVER_ROOT
case "$1" in
   start)
    exec forever  start -m 10 -o $FOREVER_ROOT/output.log -e $FOREVER_ROOT/error.log ./bin/www  ./
    ;;
   stop)
    exec forever stop ./bin/www 
    ;;
   *)
    echo "Usage: service nodeup {start|stop}"
    exit 1
    ;;
    esac
  exit 0

The problem is that when I run (with start or stop, same effect)

 sudo service nodeup start 

I get

/etc/init.d/nodeup: 12: cd: can't cd to /var/www/mydir

if I run the script with sudo /etc/init.d/nodeup start it runs perfectly, no problem.

I cannot understand what is going on, or what could have changed on my machine. Any idea of what I should check that might give now this behaviour ? or what to change in my script so it works again ? Thanks for any help


Solution

  • I changed the shell of the script from sh to bash It seems to have rendered the script more stable