Running forever within su as a different user

lwood picture lwood · Sep 22, 2013 · Viewed 7.2k times · Source

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?

Answer

Tony Chen picture Tony Chen · Sep 22, 2013

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