If you're using upstart, try this (putting it to your upstart script)
exec su -s /bin/sh -c 'exec "$0" "$@"' username -- /usr/local/bin/forever ...
reference: https://superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
Question
I am experiencing weird behavior with forever, which I want to use to keep alive my node app.
I want to run my forever processes as my regular user lwood
, not as root
.
I need to know how to run forever properly within root
mode, but as the user lwood
. (This is needed because, for example as a special case, upstart scripts run as root.)
These commands illustrate my problem (I'm on Ubuntu 12.04, and $
is regular user and #
is root):
$ su
[type in su password]
# cd /home/lwood/myapp
# sudo -u lwood forever -a -l "/home/lwood/myapp/logfile.log" start app.js
info: Forever processing file: app.js
# forever list
info: No forever processes running
# exit
$ forever list
info: No forever processes running
So forever successfully started, yet no processes are running under neither lwood nor root!
How can I fix this problem?
La solution
If you're using upstart, try this (putting it to your upstart script)
exec su -s /bin/sh -c 'exec "$0" "$@"' username -- /usr/local/bin/forever ...
reference: https://superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
Autres conseils
On systems with systemd (RHEL 7, CentOS 7, Debian, Ubuntu, Fedora, ...) you can use this script as /usr/lib/systemd/system/nodejs.service or equivalent (check for other *.service files):
[Unit]
Description=Node.js Application
After=postgresql.service network.target
Wants=postgresql.service
[Service]
ExecStart=/usr/bin/su - <user> -c '/usr/bin/npm start --prefix /path/to/app'
WorkingDirectory=/path/to/app
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
Probably because it's running as a different context on a different user. Maybe you need to add sudo a swell for the list:
sudo -u lwood forever list