14

Starting Node.js with Upstart, when trying to access files within Node.js it cannot access them without using the full path. I need it to use the working directory.

start on startup
stop on shutdown

script
        echo $$ > /var/run/mynodeapp.pid
        exec sudo -u mynodeapp node server.js >> /var/log/mynodeapp.sys.log 2>&1
end script

pre-start script
        echo "Starting" >> /var/log/mynodeapp.sys.log
end script

pre-stop script
        rm /var/run/mynodeapp.pid
        echo "Stopping" >> /var/log/mynodeapp.sys.log
end script
jbowes
  • 4,062
  • 22
  • 38
Chris Evans
  • 993
  • 2
  • 13
  • 30

2 Answers2

25

The solution is to change directory within the script. In my case, the user is mynodeapp and the node files are in the users directory (/home/mynodeapp/).

script
        chdir /home/mynodeapp/
        echo $$ > /var/run/mynodeapp.pid
        exec sudo -u mynodeapp node server.js >> /var/log/mynodeapp.sys.log 2>&1
end script

I have yet to find out what $$ means on the echo line or 2>&1. Maybe somebody could chime in with this if they know!

Chris Evans
  • 993
  • 2
  • 13
  • 30
  • 2
    Have a look here for what $$ is: http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables – jbowes Feb 12 '13 at 01:34
  • It says I can in 2 days :-/ – Chris Evans Feb 12 '13 at 01:52
  • 1
    Why do you need to manage your own logging and saving pids? Upstart manages that for you. Take a look at `/var/log/upstart/mynodeapp.log` and `status cws-frontend` command. – timurb Aug 12 '14 at 18:59
-1

You should use the chdir stanza as per the Upstart docs: http://upstart.ubuntu.com/cookbook/#chdir

Tarjei Huse
  • 1,231
  • 11
  • 14