Script to run at startup (node example)

I’ve browsed to find an easy way to make a node.js program to run at startup and stop and start easily and this is what I came up with. As I might be running multiple node.js programs I need to search on the parameters when killing the right node program.

What I’m doing is the following:

  1. Discarding stdout and stderr from the program at start
  2. Using a parameter sent to node.js to identify the right program to stop when stopping the service

Below is my /etc/init.d/rf24snserv program and don’t forget to make it executable and add the symbolic link using update.rc. I.e.

  1. sudo nano /etc/init.d/rf24snserv
  2. Add the code as below
  3. Save and exit nano
  4. sudo chmod 755 /etc/init.d/rf24snserv
  5. sudo update.rc rf24snserv defaults

Find more details on how to create a startup script here:

#! /bin/sh
# /etc/init.d/

# Provides: rf24sn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts rf24sn
# Description: A script to start the service

case "$1" in
                echo "Starting rf24sn"
                nohup /usr/bin/rf24sn -b mqtt:// --spi /dev/spidev0.0 --ce 25 --irq 24 -vvv --rate 250kbps > /dev/null 2>&1 &
                echo "Stopping rf24sn"
                ps -ef | grep rf24sn | grep -v grep | awk '{print $2}' | sudo xargs kill
                echo "Usage: /etc/init.d/rf24snserv (start|stop)"
                exit 1
exit 0

I had some issue to get “stop” to work but with the above statment it finds the PID and finally passes it on to the kill command.

This is an example output from ps -ef | grep rf24sn

root      3524     1  0 19:38 ?        00:00:02 node /usr/bin/rf24sn -b mqtt:// --spi /dev/spidev0.0 --ce 25 --irq 24 -vvv --rate 250kbps
pi        3554  3285  0 19:54 pts/0    00:00:00 grep --color=auto rf24sn