Simple upstart job in Ubuntu to ensure your processes keep running

by jagbir on August 27, 2012

This is small post explaining a simple concept in Ubuntu. One of my friend requested about a way to ensure his processes keep running. Of course there are multiple ways to do and I have documented this earlier with details my earlier post.

Let’s review quickly:

1. Run processes through cron but it’s only helpful if check frequency is per minute or higher.
2. Use some process monitoring solution like Monit.
3. Create custom daemon wrapper script which keep checking processes all the time.

Due to some path/env issue (I have not investigated that), my friend said his scripts are not running as expected under Monit or throug cron. Also cron is not an option here because he needs checking processes very frequently, let’s say every 5 seconds. This left us third option where we can create a custom wrapper script which should keep running like a daemon and keep an eye on processes. Now the question is how to ensure that our daemon keeps running all the time?

As described in my earlier blog post on Redhat based Linux, we can put entry in /etc/inittab which will ensure that our daemon will always be there in processlist but that sysV type has been discontinued in Ubuntu (12.04 I am talking about) and hence we need to create a simple process using upstart.

Let’s say our daemon script path is /root/daemon/

Create a config file for upstart job:

description     "MyMon: monitoring my custom processes."
start on startup
stop on shutdown
exec start-stop-daemon --start --make-pidfile --pidfile /var/run/ --exec /root/daemon/ >> /var/log/mymon.log 2>&1

Here, “start on startup” and “stop on shutdown” implies the process should get started when system boots up and should be gracefully terminated upon shutdown.

respawn keyword ensures that this process should be running all the time. If its crashed/stopped, it will be respawned automatically.

exec is used here to execute our script, it gives PATH and optional arguments to our script. You can use ‘script’ keyword instead to write down multiple script commands to be executed by /bin/sh. You can check full documention here.

After saving the upstart config, time to start our job, check its pid and try stopping it:

$ sudo start mymon 
mymon start/running, process 20890
$ cat /var/run/ 
$ sudo stop mymon
mymon stop/waiting

Do you also think its simple and efficient method for process monitoring? or if you have any other suggestion? would appreciate your comments.

Other helpful related articles:
* Run scripts as daemon or through cron continuously
* Bash script to backup essential log files in Linux
* Top 5 most useful tools for Linux Admin

Previous post:

Next post: