I have a node.js script which need to start at boot and run under the www-data user. During development I always started the script with:
su www-data -c 'node /var/www/php-jobs/manager.js
I saw exactly what happened, the manager.js works now great. Searching SO I found I had to place this in my /etc/rc.local
. Also, I learned to point the output to a log file and to append the 2>&1
to "redirect stderr to stdout" and it should be a daemon so the last character is a &
.
Finally, my /etc/rc.local
looks like this:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. su www-data -c 'node /var/www/php-jobs/manager.js >> /var/log/php-jobs.log 2>&1 &' exit 0
If I run this myself (sudo /etc/rc.local
): yes, it works! However, if I perform a reboot no node
process is running, the /var/log/php-jobs.log
does not exist and thus, the manager.js does not work. What is happening?
The file rc. local does not contain information on system startup components, but only superuser/root defined components. However, not all root startup programs are described in rc.
In this example of a rc.local script I use io redirection at the very first line of execution to my own log file:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exec 2> /tmp/rc.local.log # send stderr from rc.local to a log file exec 1>&2 # send stdout to the same log file set -x # tell sh to display commands before execution /opt/stuff/somefancy.error.script.sh exit 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With