linux + create simple watch dog process in shell script

I need to create watch dog process (will run in linux version 5.x) that look all time on /etc/cluster.cf file

And if the process matches the string: machineA_is_active in the cluster.cf file

Then this process will execute other script

My question is – how to run this process in way that process will up and running all time that Linux is up –

and in case this process is down need to startup this process again

So please advice what the basic structure for this scenario ?

(I will happy if I get real example)


I'd not recommend trying to keep a process running all the time to do this. There are more simple methods. Your machine should have cron running which is a periodic task scheduler. You can schedule a process to run periodically, as frequently as once per minute to check the contents of the file and do what needs to be done. You might add something like this to the crontab:

* * * * * /path/to/yourscript

see man 1 crontab and man 5 crontab and man 8 cron for more information about cron.

Even better is to use incron which allows you to specify a process to be run any time this file is changed. If you have incron installed you'd add something like this to the incrontab:

/etc/cluster.cf IN_MODIFY /path/to/your/script

Saying that anytime /etc/cluster.cf is modified, run your script. see man 5 incrontab and man 1 incrontab


Assuming you are using a SysV distribution, create an initialization script and place it in /etc/init.d.

Look at any of the scripts that are already there for examples of how to format this script. Consider the ones that use the daemon function. You would then use chkconfig to enable the script to run at boot. This initialization script should write it's PID into a lock file. You will need a second 'helper process' to check for the first one's PID by reading the lock file and determine whether or not it is running. Include logic to destroy the lock and restart the first process if it does not find a running PID.