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:
see
man 1 crontab
andman 5 crontab
andman 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:
Saying that anytime /etc/cluster.cf is modified, run your script. see
man 5 incrontab
andman 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.There are a thing whose only job is to (re)start other things, its name is
init
, and it is configured throughinittab
. To make something really immortal, add it to inittab withrespawn
option.A simple checker script (a candidate for
inittab
) might be like this:a very simple solution (not the most elegant) for a watchdog is to create a cron job that executes a service that checks for another service's status.
then put this line at the end of your crontab file (attention this will run evry minute of every day until you change crontab again)
then CTRL-X, Y, ENTER and reboot your machine.
this is an example of such a service: