Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upstart node.js working directory

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
like image 376
Chris Evans Avatar asked Feb 12 '13 01:02

Chris Evans


1 Answers

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!

like image 146
Chris Evans Avatar answered Sep 27 '22 21:09

Chris Evans