0

My machine is Ubuntu 16.04. I want to start my node.js application when the machine boots up everytime. According to this post about autostarting services. I tried to make a service by doing the following:

1) I created a script to start node app with node's forever module.

#!/bin/bash

echo "Starting App"

forever -a start /opt/app/app.js

echo "App started"

2) I named this script startApp.sh and put this script inside /etc/init.d/ folder.

3) I ran the command update-rc.d startApp defaults

But I got the error update-rc.d: error: initscript does not exist: /etc/init.d/startApp

What did I do wrong?

  • Notice the discrepancy between: "I named this script startApp.sh and put this script inside /etc/init.d/ folder." and "initscript does not exist: /etc/init.d/startApp". .sh is missing in second case, so you probably need to add it when launching update-rc.d – Patrick Mevzek May 3 '18 at 22:38
  • @PatrickMevzek that makes sense but for some reason, the link in this post did not include a .sh so I assumed there was some conversion that is done when running this command – b11 May 4 '18 at 13:40
0

Highly advice you to use the init.d 'standard' of writing services, this also create less confusion when another person is looking at it that is aware of init.d services. Please see the example below:

#!/bin/sh
#
# Author: Your Name <email@address.domain>
#
### BEGIN INIT INFO
# Provides:       Name
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $syslog
# Should-Stop:    $syslog
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: Add a description here
### END INIT INFO

# Using LSB functions:
. /lib/lsb/init-functions

set -e

NAME="NAME"
DAEMON=/opt/app/app.js
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Function that starts the daemon/service.
d_start() {
        log_daemon_msg "Starting" "$NAME"
        start-stop-daemon --start --quiet --background --pidfile $PIDFILE --make-pidfile --startas $DAEMON
        log_end_msg $?
}

# Function that stops the daemon/service.                                         
d_stop() {                                                                        
        log_daemon_msg ":: stopping" "$NAME"                                      
        start-stop-daemon --stop --pidfile $PIDFILE --retry 10                    
        log_end_msg $?                                                            
}                                                                                 

# Function that sends a SIGHUP to the daemon/service.                             
case "$1" in                                                                      
  start|stop)                                                                     
        d_${1}                                                                    
        ;;                                                                        
  restart|reload|force-reload)                                                    
        d_stop                                                                    
        sleep 1                                                                   
        d_start                                                                   
        ;;                                                                        
  status)                                                                         
        status_of_proc "$NAME" "$DAEMON" && exit 0 || exit $?                     
        ;;                                                                     
  *)                                                                           
        echo "Usage: $SCRIPTNAME {start|stop|restart|status}"                  
        exit 3                                                                 
        ;;                                                                     
esac                                                                           

exit 0

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.