How to start Script as Daemon – DEBIAN – UBUNTU
########################################

Note this is from this article: http://xmodulo.com/2013/01/how-to-automatically-start-program-on-boot-in-debian.html

Note: didnt test in latest Ubuntu but should work

This is how you can start a script or a service on boot and turn it off on shutdown.

NOTE: All scripts below should use full paths, or variables that point to full path
NOTE: if you get LSB warning when doing update-rc.d foobar defaults, realize its just a warning
NOTE: use /bin/sh not /bin/bash in the service script (although im sure bash is fine), its just I checked thru all of my /etc/init.d service files on my debian system and out 60 scripts, 2 used #!/bin/bash and 58 used #!/bin/sh

NOTE: read the comments to learn some good etiquette
NOTE: generally the comments in init files have a format followed that should look something like this but we are skipping it (its explained in depth in /etc/init.d/README):

### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $portmap
# Should-Stop: $portmap
# X-Start-Before: nis
# X-Stop-After: nis
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d.
### END INIT INFO<span style="font-family: Lato, sans-serif; font-size: 15.555556297302246px; line-height: 1.5; background-color: #ffffff;"> </span>

PROGRAM THAT RUNS QUICK
=========================

This is for programs that run once and quick like “date”. The point is that it runs once.

Note: service script start
Will start everything as child of sysvinitd so it will not be tied to shell (it will bel ike starting program with nohup) so that you can close shell and program still runs

If starting that runs once on boot:

Be root

# sudo -i
# vi /etc/init.d/foobar-once
#!/bin/sh
#Filename: /etc/init.d/foobar-once

# The following part always gets executed.
echo "some words and operations and variable settings"

# The following part carries out specific functions depending on arguments.
case "$1" in
start)
echo "Starting foobar"
echo "do some program once, like: date"
date
;;
stop)
echo "Stopping foobar"
echo "stop a process, like: date - although date doesnt run long enough to ever need to be stopped"
killall date
;;
*)
echo "Usage: /etc/init.d/foobar-once {start|stop}"
exit 1
;;
esac

exit 0
# chmod 755 /etc/init.d/foobar-once

Now to start the service you can

# /etc/init.d/foobar-once

Or

# service foobar-once start

So make it executable like so

To ensure this runs on start up (in systemd terms, to enable service):

# update-rc.d foobar-once defaults

To remove it from starting up (in systemd terms, to disable service):

update-rc.d -f foobar-once remove

PROGRAM THAT RUN FOREVER
=========================

Programs that run forever like daemons or services. Or a looping script.
I have a looping script that checks if cron is running. And if cron dies, it restarts it.

My cron script to monitor cron and turn it on if its dead (it just turn it on every 5 seconds, but if you start cron thru services cron wont start again if its running)

vi /root/monitor/monitcron.sh
#!/bin/sh
#
# Note whatever this outputs, will not be shown on screen because it gets started by the init process on boot, plus we shut this process up with > /dev/null 2> /dev/null
# if this needs to say anything important, which it does it can go to /var/log/monitor-cron.log
#
# starts cron if needed, if cron already started, it wont start it again
INTERVAL1=5
echo >> /var/log/monitor-cron.log
echo "==== Monitoring Cron ==== - DATE: `date` `date +%s`" >> /var/log/monitor-cron.log
echo "every dot, is $INTERVAL1 seconds passed, and checked for cron" >> /var/log/monitor-cron.log
echo "if cron is on it will not turn it on" >> /var/log/monitor-cron.log
echo "if cron is off it will try to turn it on with 'service cron start'" >> /var/log/monitor-cron.log
while true; do
service cron start
echo -n "." >> /var/log/monitor-cron.log
done

My service file:

vi /etc/init.d/monitor-cron
#!/bin/sh
#Filename: /etc/init.d/monitor-cron

# The following part always gets executed.
echo "Monitor Cron Service"

# The following part carries out specific functions depending on arguments.
case "$1" in
start)
echo "Monitor Cron Service - STARTED"
# if this file continously outputs to the shell, you should smake it be quiet with > /dev/null and 2> /dev/null
# if it needs to report anything, have it save logs to /var/log (like i do with it to /var/log/monitor-cron.log)
/root/monitor/monitcron.sh 2> /dev/null > /dev/null
;;
stop)
echo "Monitor Cron Service - STOPPED"
killall monitcron.sh
;;
*)
echo "Usage: /etc/init.d/monitor-cron {start|stop}"
exit 1
;;
esac

exit 0
# chmod 755 /etc/init.d/monitor-cron

To enable it (so it turn on during boot up):

# update-rc.d monitor-cron defaults

To enable it (so it doesnt turn on during boot up):

# update-rc.d -f monitor-cron remove

NOTE:
If I started my script like so:

# /root/monitor/monitcron.sh

It will die when I turn off shell/terminal/putty session

If I started my script like so:

# nohup /root/monitor/monitcron.sh &

It wants me to press annoyingly
With # pstree it will look like its tied to bash, but when you turn off the terminal it will quickly become a child of PID 1 (init), thus persistenting closing shells

If I started service like so

# /etc/init.d/monitor-cron start

It will also die when I turn off my shell/terminal/putty
You can check with # pstree
You will see its tied to bash

However If I started service like so:

# service monitor-cron start

Then it will be tied to PID1 (init process) and thus persist thru closing of shells and terminal. It will always be alive untill I kill it or close it with service monitor-cron stop
With # pstree you will see its tied to PID 1

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *