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)

link|improve this question

What is your Linux distribution? – Aaron Copley Jan 19 at 20:41
feedback

3 Answers

up vote 2 down vote accepted

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

link|improve this answer
yes, but who will guard cron? – yarek Jan 19 at 22:07
feedback

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.

link|improve this answer
feedback

There are a thing whose only job is to (re)start other things, its name is init, and it is configured through inittab. To make something really immortal, add it to inittab with respawn option.

A simple checker script (a candidate for inittab) might be like this:

while :
do
     grep -q machineA_is_active /etc/cluster.cf && activation_script
     # here one needs to ensure the above wan't fire again
     # - say, by carelessly wiping off /etc/cluster.cf
     # or carefully editing out the triggering record
     sleep $delay
done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.