Persistent Service Daemon Processes

In order to start up a service (such as an Unframed Architecture TCP/IP server daemon), a shell script, regularly run by the cron system, can be used to make it start up whenever the service goes down, regardless of the underlying cause. You will need to use sudo for editing the crontab file and making the script executable.

Create a shell script for restarting the ‘daemon’ service if it is found to not be running (script path is “/usr/sbin/daemon-p.bash”):

#!/bin/bash

# find a process name that starts with '/' and 
# explicitly matches 'daemon' and 
# is at the end of the string ('$'):
ps ax | grep '/daemon$' | sed -e 's/.*grep.*//' | grep '/daemon$' 
if (( "$?" != 0 )) ; then
	echo "restarting daemon at $(date)" >> /tmp/daemon-p.log
	nohup /usr/sbin/daemon > /dev/null 2>&1 &
	ps ax | grep '/daemon$' | sed -e 's/.*grep.*//' | grep '/daemon$' 
	if (( "$?" == 0 )) ; then
		echo "restart successful at $(date)" >> /tmp/daemon-p.log
	else
		echo "restart failed at $(date)" >> /tmp/daemon-p.log
	fi
fi

Make the script executable:

chmod +x /usr/sbin/daemon-p.bash

Edit the crontab file using nano (or your favorite editor):

EDITOR=nano crontab -e

Add the following line to the end of the crontab file (will run the ‘daemon-p.bash’ script every minute):

*/1 * * * * /usr/sbin/daemon-p.bash

(see CronHowTo for other temporal options)

Run the following command to start your service daemon:

nohup /usr/sbin/daemon > /dev/null 2>&1 &

Run the following command to get the PID of your service daemon:

ps ax | grep '/daemon$' | sed -e 's/.*grep.*//' | grep '/daemon$'

Returns:

(PID) ?        S      0:00 /usr/sbin/daemon

Run the following command simulate the service daemon going down:

kill (PID)

Wait until the next minute (on the system clock) elapses, and test for your service daemon coming back to life:

ps ax | grep '/daemon$' | sed -e 's/.*grep.*//' | grep '/daemon$'

Returns (when the service daemon has restarted):

(PID) ?        S      0:00 /usr/sbin/daemon

Debugging

View the log file (produced by the script) to see the history:

cat /tmp/daemon-p.log

Returns something like this:

restarting daemon at Tue Jun  5 15:26:01 MDT 2018
restart successful at Tue Jun  5 15:26:01 MDT 2018

If the process detection phase of the script seems to be failing, shunt its output to the log file by modifying the lines that detect the process:

ps ax | grep '/daemon$' | sed -e 's/.*grep.*//' | grep '/daemon$' >> /tmp/daemon-p.log

Launch, Monitor, and Terminate Linux Daemons

If you have a daemon listening on port 6666,

connector = 
 accept(
  listener, 
  (struct sockaddr*)&client, 
  (socklen_t*)&acceptor);

the following commands will be helpful for managing it.

Launch:

nohup ./daemon > /dev/null 2>&1 &

Monitor (use sudo if not root):

netstat -lpn |grep :6666

Above call returns (note the returned PID):

tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN PID/daemon

Terminate:

kill -9 PID