I am trying to run a node app on boot. The script runs ok from the command line, I can stop and start it but when I try to do it on boot I get an error as if the script were reading the node_app.js line by line.
Here is my script my_node_server:
#! /bin/bash
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO
# Some things that run always
APP_DIR="/home/pi/node_app_folder"
NODE_APP="node_app.js"
LOG_FILE="/home/pi/my_node_server.log"
PID_FILE="/home/pi/my_node_server.pid"
NODE_EXEC=$(which node)
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting script my_node_server "
echo "Check log in /home/pi/my_node_server.log"
echo "" > "$LOG_FILE"
echo "" > "$PID_FILE"
"$NODE_EXEC" "$APP_DIR/$NODE_APP" > "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "my_node_server is working with pid: $(cat $PID_FILE)" | tee "$LOG_FILE"
echo "Check pid in /home/pi/my_node_server.pid"
;;
stop)
echo "Stopping script my_node_server" | tee -a "$LOG_FILE"
PID="$(cat "$PID_FILE")"
echo killing PID: "$PID"
kill "$PID"
;;
*)
echo "Usage: /etc/init.d/my_node_server {start|stop}"
exit 1
;;
esac
exit 0
It also works on the command line, from both pi and root users:
APP_DIR="/home/pi/node_app_folder"
NODE_APP="node_app.js"
NODE_PROGRAM=$(which node)
"$NODE_PROGRAM" "$APP_DIR/$NODE_APP" 2>&1
Why does it not do it at boot time? I have done:
sudo update-rc.d my_node_server defaults
By the way my shell is bash:
$ echo $0
/bin/bash
If I replace
"$NODE_EXEC" "$APP_DIR/$NODE_APP" > "$LOG_FILE" 2>&1 &
for
/home/pi/my-node-dir/bin/node /home/pi/node_app.js > "$LOG_FILE" 2>&1 &
it works ok.
So, what am I missing?