I want to keep a nodejs app running, so I followed this: http://howtonode.org/deploying-node-upstart-monit and installed Monit and Upstart which have been working perfectly for a couple of months. I just added a feature to my app that allows disk writes and am getting permissions errors. I went looking for everything because it seemed the permissions errors shouldn't be the problem. It turns out that the user that I start with Upstart is not actually the user that is running the app!
When I run:
sudo -u deploy NODE_ENV=production node /srv/www/[name of my app]/dist/app.js
Either via Monit/Upstart, or just via SSH, it creates 2 processes. From ps ax
:
2869 ? Ss 0:00 sudo -u deploy node /srv/www/[name of my app]/dist/app.js
2878 ? Sl 0:01 node /srv/www/[name of my app]/dist/app.js
When I log in as the deploy
user and just run:
NODE_ENV=production node /srv/www/[name of my app]/dist/app.js
There is only 1 process!
3131 pts/1 Sl+ 0:04 node /srv/www/[name of my app]/dist/app.js
This issue is that for some reason, the second process doesn't have the correct permissions to write and seems to be the one doing the work. Everything is perfect when I don't use Upstart including the writes, but I need Upstart/Monit to keep my nodejs app running.
What's the best way to configue Monit/Upstart in this case, then? I want to keep low permissions (hence the deploy
user which does not have sudo) and I want the app to start when it crashes.
This is my Upstart script:
start on startup
stop on shutdown
script
export HOME="/home/deploy"
echo $$ > /var/run/[name of my app].pid
exec sudo -u deploy NODE_ENV=production node /srv/www/[name of my app]/dist/app.js >> /srv/www/log/[name of my app].sys.log 2>&1
end script
Anything wrong there?
The tutorial says to use sudo -u user ...
in order to run it as the user you want. But that's the thing that's causing the problem.
Thanks!
Paul