środa, 3 sierpnia 2011

Ubuntu: startup process respawn

Today I had a discussion with one of my colleagues about mechanisms for monitoring the process and respawning it if it dies. We discussed the approach via inittab or home made solution with external process monitor. I decided to check it on my ubuntu 11.04 Natty box and to my surprise there was not /etc/inittab file. After deeper investigation it seems that the upstart init daemon in ubuntu is configured via files located in the /etc/init director. Moreover it seems to offer a great piece of functionality that we needed, e.g. limiting the number of respawns in specific period of time.
The startup of services is controlled via the start command, which actually is a symbolic link to the initctl - please check the man for details.
I decided to check also if the number of respawns functionality really works - for that purpose I prepared a configuration file as below:

root@krystianek:/etc/init.d# cat /etc/init/test_respawn.conf 
description "Test respawn"


start on filesystem or runlevel [2345]
stop on runlevel [!2345]


respawn
respawn limit 10 5
umask 022


pre-start script
end script


exec /etc/init.d/test_respawn.sh
root@krystianek:/etc/init.d# 

Limiting the number of respawns to 10 in timeframe of 5 seconds. Next I prepared the test_respawn.sh script to count the actual number of restarts as below:

root@krystianek:/etc/init.d# cat /etc/init.d/test_respawn.sh 
#!/bin/sh


declare -i VALUE
echo "Testing respawn feature"
TMP_FILE=/tmp/count
if [ -f "$TMP_FILE" ]; then
  VALUE=`cat ${TMP_FILE}`
else
  VALUE=0
fi


VALUE=`expr $VALUE + 1`
echo $VALUE > ${TMP_FILE}
exit 1

The result shall be stored in the /tmp/count file. Now as everything is ready for the test it is time to do it:

root@krystianek:/etc/init.d# file /tmp/count
/tmp/count: ERROR: cannot open `/tmp/count' (No such file or directory)
root@krystianek:/etc/init.d# start test_respawn
test_respawn start/pre-start, process 8293
root@krystianek:/etc/init.d# cat /tmp/count
11

Voila the result is 11 (1 start attempt + 10 respawns). 

Brak komentarzy:

Prześlij komentarz