How to start a Node.js app on system boot? - node.js

I'm working on a Raspberry Pi running Raspbian running a Node.js app and trying to get it to start when the Pi boots. I found a couple of examples but I can't seem to get it working. My current code is:
#! /bin/sh
# /etc/init.d/MyApp
### BEGIN INIT INFO
# Provides: MyApp.js
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts MyApp.js
# Description: Start / stop MyApp.js at boot / shutdown.
### END INIT INFO
# If you want a command to always run, put it here
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting MyApp.js"
# run application you want to start
node /home/pi/app/MyApp/MyApp.js
;;
stop)
echo "Stopping MyApp.js"
# kill application you want to stop
killall MyApp.js
;;
*)
echo "Usage: /etc/init.d/MyApp {start|stop}"
exit 1
;;
esac
exit 0
I have this in the etc/init.d folder, ran chmod +x /etc/init.d/MyApp, I'm able to run it manually, then I run sudo update-rc.d MyApp defaults, reboot and the script never runs. I've looked at some different examples, made adjustments and still no luck.

I solved this problem by first checking where node.js was installed on RaspberryPi:
which node
This gave me :
/usr/local/bin/node
Open crontab config:
sudo crontab -e
Then in my crontab :
#reboot sudo /usr/local/bin/node <complete path to your .js app> &
Save, reboot, and problem solved !

Mohit is right, but just for clarification, you can use readlink to find the full path for your Node.js app as it will be needed later to add as a cron job.
readlink -f <<name of file >>
For instance readlink -f HAP-NodeJS/Core.js results in /home/pi/HAP-NodeJS/Core.js
You can also use which node to find the full path where node.js is installed
Next, create a new cron job using sudo crontab -e and add the following code at the very end:
#reboot sudo /usr/local/bin/node <<.js application path>> &
for instance, my code looks like this.
#reboot sudo /usr/local/bin/node /home/pi/HAP-NodeJS/Core.js &
Upon reboot (or start up) , your Node.js should run. Hope this clears things.

If you're using a prebuilt Pi release like 0.10.24, you may be experiencing a PATH issue.
You can either provide the full path to the node binary as part of the start command or make sure the PATH to the node binaries are set before /etc/init.d/MyApp is ran. I had the same issue and tried both with success. Also, the stop command as you have it may not be working.
#! /bin/sh
# /etc/init.d/test
### BEGIN INIT INFO
# Provides: test
# 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
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting test.js"
# run application you want to start
#node /home/pi/test.js > /home/pi/test.log
/home/pi/downloads/node-v0.10.24-linux-arm-pi/bin/node /home/pi/test.js >> /home/pi/test.log
;;
stop)
echo "Stopping test.js"
# kill application you want to stop
killall -9 node
# Not a great approach for running
# multiple node instances
;;
*)
echo "Usage: /etc/init.d/test {start|stop}"
exit 1
;;
esac
exit 0
If you'd like to do sudo node, you can add the PATH to Defaults secure_path using sudo visudo.
Also, I would recommend using something like forever to keep your process running after crashes and what not.

Related

Unable to start Node on system reboot Ubuntu Crontab

I have tried this with adding the forever start code in /etc/rc.local didn't work.
When I use the #reboot keyword in /etc/rc.local it says #reboot cannot be found.
So I went back to using crontab Here is my crontab script. All other crontabs are working except the reboot one. In syslog, it says
Jun 4 09:51:12 ip-172-31-28-35 /usr/sbin/irqbalance: Balancing is ineffective on systems with a single cache domain. Shutting down
Jun 4 09:51:12 ip-172-31-28-35 cron[959]: (CRON) STARTUP (fork ok)
Jun 4 09:51:12 ip-172-31-28-35 cron[959]: (CRON) INFO (Running #reboot jobs)
Jun 4 09:51:12 ip-172-31-28-35 CRON[1005]: (ubuntu) CMD (/usr/bin/sudo -u ubuntu /usr/local/bin/forever start home/ubuntu/chat2/index.js)
Which shows that the reboot command in my cron tab is working but for some reason forever is still not starting node. After reboot , I run forever list and it says No forever processes running
I am assuming the problem is somehow with the node and forever paths. I am new to this and dont know which exact path to use on this statement in crontab.
I have also tried the following:
#reboot /usr/local/bin/forever start -c /usr/local/bin/node /home/ubuntu/chat2/index.js
and
#reboot /usr/local/bin/forever start /home/ubuntu/chat2/index.js
None of these are working.
If I run which forever it says
/usr/local/bin/forever
If I run which node it says
/usr/local/bin/node
If I get the full path of my index.js app file, by doing readlink -f index.js in my chat2 directory it says
/home/ubuntu/chat2/index.js
I just want to run this command every time my system reboots. I want to start my node app. The following line works perfect when I cd to the chat2 directory manually. I want this to work on reboot itself.
forever -m5000 -w start index.js
You can create a service with you code instead of using cron. Actually I prefer that because you can stop or start it whenever you want and you can also run it on the system reboot or start.
So:
1- Create a service in /etc/init.d/name_of_file
#!/bin/bash
#/etc/init.d/name_of_file
### BEGIN INIT INFO
# Provides: name
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: add service
# Description:
#
### END INIT INFO
# Some things that run always
case "$1" in
start)
echo "Starting app_name "
touch /var/lock/app_name
cd /where/is/your/file
node index.js &
;;
stop)
echo " Stopping "
rm /var/lock/app_name
sudo pkill -f node
;;
status)
if [ -e /var/lock/app_name ]
then
echo "app_name is running"
else
echo "app_name is not running"
fi
;;
*)
echo "Usage:service app_name{start|stop|status}"
exit 1
;;
esac
exit 0
So after that you have created a service for running you nodejs application.
You have to give running permission to that script
chmod +x /etc/init.d/app_name
Now the only thing you have to do is configure this to run on boot.
Run:
sudo update-rc.d app_name defaults
And then every time you reboot you computer the service will start itself.
Suggest redirect stdout/stderr to file to debug why your script in crontab not work:
/usr/local/bin/forever start -c /usr/local/bin/node /home/ubuntu/chat2/index.js >/tmp/forever.log 2>&1 &
See log file for details after reboot.
You also can try pm2 , like forever but support buildin system start script generate, and will launch your apps after reboot.

Unable to use service command with debian 8 (Jessie)

In order to install LibreOffice 4.4 into my Debian 8 (Jessie), I just got all my bash scripts from my Debian 7.5 and run them into the same way into the D8 one.
I know there was several changes into the new version but I'm not able to use my service like this anymore :
sudo service libreoffice start
When doing this doesn't start anything and I have to start it using :
sudo /etc/init.d/libreoffice start
And strange thing, when doing (bad parameter) :
sudo service libreoffice dzedjiodjzedj
...the script is perfectly executed and it displays my catched error
Here is my /etc/init.d/libreoffice file :
#
# libreoffice This shell script takes care of starting and stopping the LibreOffice Daemon
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: libreofficedaemon
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Init.d script to run a LibreOffice Daemon
# Short-Description: start and stop LibreOffice Daemon
### END INIT INFO
NAME="LibreOffice Service"
LIBREOFFICE_HOME=/opt/libreoffice4.4
LIBREOFFICE_USER=libreoffice
export LIBREOFFICE_HOME LIBREOFFICE_USER
start() {
echo -ne "Starting $NAME. \n"
su $LIBREOFFICE_USER -c "$LIBREOFFICE_HOME/start.sh"
}
stop() {
echo -ne "Stopping $NAME. \n"
su $LIBREOFFICE_USER -c "$LIBREOFFICE_HOME/stop.sh"
}
kill() {
echo -ne "Force close of $NAME. "
killall -u $LIBREOFFICE_USER
}
cd $LIBREOFFICE_HOME
case "$1" in
start|stop)
$1;;
restart) stop; start;;
kill) kill;;
*)
echo "Usage: /etc/init.d/libreoffice {start|stop|restart|kill}"
exit 1
;;
esac
exit 0
And I just run that issue with tomcat8 service yesterday, I just started manually the service and sudo service tomcat8 start worked after that but nothing for libreoffice one..
From the Debian Jessie Release Notes :
When you are asked if any file in the /etc/init.d directory, or the /etc/manpath.config file should be replaced by the package maintainer's version, it's usually necessary to answer “yes” to ensure system consistency
With systemd you now have to use systemctl:
sudo systemctl start libreoffice
Here's some more info

Starting node app at startup on raspberry pi

EDIT: As per Jim Rush's advice I'm now using rc.local instead of init.d direclty to run forever start on boot up.
I'm wracking my head on this one.
I'm wanting to start a node app on the raspberry pi startup and reboot. I'm using forever to actually call the app and using init.d for the debian style start instructions.
I've created the kuuyi file within the /etc/init.d directory, given it a permission of 755 and, after editing the file, ran update-rc.d kuuyi defaults to hopefully trigger Raspbian to start it on restart/boot.
Here's my init.d file:
#!/bin/sh
#/etc/init.d/kuuyi
### BEGIN INIT INFO
# Provides: kuuyi
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Kuuyi
### END INIT INFO
case "$1" in
start)
/usr/local/bin/forever --sourceDir=/home/pi/kuuyi_device -p /root/.forever run.js
;;
stop)
/usr/local/bin/forever stop --sourceDir=/home/pi/kuuyi_device run.js
;;
*)
echo "Usage: /etc/init.d/kuuyi {start|stop}"
exit 1
;;
esac
exit 0
Any ideas as to why this isn't working? I'm running Raspbian on a Raspberry Pi B+. I've run /etc/init.d kuuyi start and forever kicks and begins the app just fine. Its just not happening after booting up the machine.
Any help on this is so appreciated, I'm about as wrung out as an old cheese cloth after dairy day on this one.
I run node (actually nodemon) from /etc/rc.local. Just the command line with & at the end. I also redirect stderr and stdout to log files to troubleshoot startup and crash problems. Getting the permissions right, on any directory that was written to, was one of my early problems.
Example:
PATH=$PATH:/opt/node/bin
cd /var/node/RoadsterNode
/opt/node/bin/nodemon /var/node/RoadsterNode/app.js < /dev/null >/var/tmp/startup.log 2>/var/tmp/startup.err &

Run script at boot after MySQL started

I want to start Seafile (cloud-server which needs MySQL) at the boot of my Raspberry Pi. My Problem is, that the Seafile starts befor mysql and caused many errors, because seafile needs mysql.
I took the recommended script:
#! /bin/sh
# /etc/init.d/seafile
### BEGIN INIT INFO
# Provides: seafile
# Required-Start: $local_fs $remote_fs $network mysql
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Script to start/stop/restart seafile
# Description: Simple script to start, stop or restart seafile for the cloud
### END INIT INFO
# Change the value of "user" to your linux user name
user=chromo
# Change the value of "script_path" to your path of seafile installation
seafile_dir=/home/chromo/cloud
script_path=${seafile_dir}/seafile-server-latest
seafile_init_log=${seafile_dir}/logs/seafile.init.log
seahub_init_log=${seafile_dir}/logs/seahub.init.log
# Change the value of fastcgi to true if fastcgi is to be used
fastcgi=true
# Set the port of fastcgi, default is 8000. Change it if you need different.
fastcgi_port=8000
case "$1" in
start)
sudo -u ${user} ${script_path}/seafile.sh start >> ${seafile_init_log}
if [ $fastcgi = true ];
then
sudo -u ${user} ${script_path}/seahub.sh start-fastcgi ${fastcgi_port} >> ${seahub_init_log}
else
sudo -u ${user} ${script_path}/seahub.sh start >> ${seahub_init_log}
fi
;;
restart)
sudo -u ${user} ${script_path}/seafile.sh restart >> ${seafile_init_log}
if [ $fastcgi = true ];
then
sudo -u ${user} ${script_path}/seahub.sh restart-fastcgi ${fastcgi_port} >> ${seahub_init_log}
else
sudo -u ${user} ${script_path}/seahub.sh restart >> ${seahub_init_log}
fi
;;
stop)
sudo -u ${user} ${script_path}/seafile.sh $1 >> ${seafile_init_log}
sudo -u ${user} ${script_path}/seahub.sh $1 >> ${seahub_init_log}
;;
*)
echo "Usage: /etc/init.d/seafile {start|stop|restart}"
exit 1
;;
esac
Can someone help me?
The best thing would be to set priority for starting these services in /etc/rc[runlevel].d file.
In your case, the first thing to check would be the runlevel to which you have boot into. You can check this using the command "runlevel". You can also check
Say, you have been booted in to runlevel 3. You can rename the current seafile file in directory "/etc/rc3.d".
For eg:
If the two files are
/etc/rc3.d/20seafile
/etc/rc3.d/50mysql
Rename the file as 70seafile or anything higher than 50.
This should fix the issue you are facing now.
Another workaround will be removing the seafile link to /etc/init.d directory and put a line saying
/etc/init.d/seafile start
inside the file /etc/rc.local
Please check this and let me know if it has fixed the issue for you.

dpkg remove to stop processes

I am currently running Ubuntu 12.04. I've created a debian package that currently installs successfully and starts three new processes. I have also made these three processes start at runtime by placing the following script inside /etc/init.d:
# This example is from http://www.debian-administration.org/article/Making_scripts_run_at_boot_time_with_Debian
# Also used http://wiki.debian.org/LSBInitScripts/
### BEGIN INIT INFO
# Provides: bleh
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
# Carry out specific functions when asked to by the system
case "$1" in
start)
cd //opt/bleh
attrf=.gatewayattributes
if [ ! -z "$1" ]
then
echo "[gateway]" >> $attrf
echo "activationKey = $1" >> $attrf
fi
./bleh1 -n &
./bleh2 &
python bleh3 &
;;
stop)
cd //opt/bleh
/usr/bin/pkill -f ./bleh1 -n
/usr/bin/pkill -f bleh3
kill -9 $(pidof bleh2)
rm -rf logs
;;
This script does start the three processes at runtime, but for some reason I cannot actually use the start/stop commands, as in sudo /etc/init.d bleh.sh stop.
An even bigger issue is that removing this package using the command:
sudo dpkg -r bleh
Does not actually stop the three processes, it only tries to remove the bleh directory I installed in my opt folder. Also, I have a folder inside my bleh directory which does not get removed, it gives me a warning stating:
Removing bleh ...
dpkg: warning: while removing bleh, directory '/opt/bleh/logs' not empty so not removed.
The files inside of that logs directory are read-only unless you have SU priviledges, but I don't see how that should be a problem as I am calling sudo on that dpkg -r command.
If I run sudo dpkg -r bleh again, it states there's no installed package matching bleh, meaning it thinks it has successfully removed the installed package, even with that exisiting logs directory and the three processes which are still running.
Sorry, I know this was long, but I could really use some help.. thanks in advance!
As recommended by the Debian New Maintainer's Guide, please use dh_installinit (building your whole package with debhelper, of course). By default, this will add scripts to start and stop on package installation and removal.
Auxiliary files (such as configuration) are usually removed in purge (e.g. dpkg -P) state. To handle this yourself, you need a deconfigure script.
Also, it is highly preferable to use start-stop-daemon instead of &, which is insufficient for proper daemonization.

Resources