我需要在/etc/cluster.cf文件中创build看门狗进程(将在Linux版本5.x中运行)
如果进程匹配cluster.cf文件中的string:machineA_is_active
然后这个过程将执行其他脚本
我的问题是 – 如何运行这个进程的过程将启动和运行的所有时间,Linux是起来的 –
如果这个过程停止,需要重新启动这个过程
那么请咨询一下这个场景的基本结构?
(如果得到真实的例子,我会高兴的)
我不build议试图保持一直在运行的进程来执行此操作。 有更简单的方法。 你的机器应该有cron运行,这是一个周期性的任务调度器。 您可以安排一个进程定期运行,就像每分钟一次一样检查文件的内容,并做好需要做的事情。 你可以把这样的东西添加到crontab中:
* * * * * /path/to/yourscript
有关cron的更多信息,请参阅man 1 crontab和man 5 crontab和man 8 cron cron。
更好的是使用incron,它允许你指定一个进程在任何时候这个文件被改变。 如果你安装了incron,你可以在incrontab中添加这样的内容:
/etc/cluster.cf IN_MODIFY /path/to/your/script
说任何时候/etc/cluster.cf被修改,运行你的脚本。 见man 5 incrontab和man 1 incrontab
假设你正在使用SysV发行版,创build一个初始化脚本并把它放在/etc/init.d中。
查看已经存在的任何脚本,以了解如何格式化此脚本的示例。 考虑那些使用守护进程function的人。 然后你将使用chkconfig来启动脚本在启动时运行。 这个初始化脚本应该把它的PID写入一个locking文件。 您将需要第二个“帮助程序”通过读取locking文件来检查第一个PID是否正在运行。 包含逻辑来销毁locking,并在没有find正在运行的PID时重新启动第一个进程。
有一件事唯一的工作就是(重新)启动其他的东西,它的名字是init ,并且它是通过inittabconfiguration的。 为了使真正的不死之物,使用respawn选项将其添加到inittab。
一个简单的检查器脚本( inittab的候选者)可能是这样的:
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
看门狗的一个非常简单的解决scheme(不是最优雅的)是创build一个cron作业来执行一个服务来检查另一个服务的状态。
sudo crontab -e
然后把这行放在你的crontab文件的末尾(注意这将每天运行一分钟,直到你再次改变crontab)
* * * * * /usr/sbin/sample_service
然后CTRL-X,Y,ENTER并重新启动你的机器。
这是一个这样的服务的例子:
#!/bin/bash # # watchdog # # Run as a cron job to keep an eye on what_to_monitor which should always # be running. Restart what_to_monitor and send notification as needed. # # This needs to be run as root or a user that can start system services. # # Revisions: 0.1 (20100506), 0.2 (20100507) NAME=sample_service NAME2=sample_service2 START=/usr/sbin/$NAME START2=/usr/sbin/$NAME2 [email protected] [email protected] GREP=/bin/grep PS=/bin/ps NOP=/bin/true DATE=/bin/date # MAIL=/bin/mail RM=/bin/rm $PS -ef|$GREP -v grep|$GREP $NAME >/dev/null 2>&1 case "$?" in 0) # It is running in this case so we do nothing. echo "$NAME is RUNNING OK. Relax." $NOP ;; 1) echo "$NAME is NOT RUNNING. Starting $NAME and sending notices." $START 2>&1 >/dev/null & NOTICE=/tmp/watchdog.txt echo "$NAME was not running and was started on `$DATE`" > $NOTICE # $MAIL -n -s "watchdog notice" -c $NOTIFYCC $NOTIFY < $NOTICE $RM -f $NOTICE ;; esac # GT06 $PS -ef|$GREP -v grep|$GREP $NAME2 >/dev/null 2>&1 case "$?" in 0) # It is running in this case so we do nothing. echo "$NAME2 is RUNNING OK. Relax." $NOP ;; 1) echo "$NAME2 is NOT RUNNING. Starting $NAME2 and sending notices." $START2 2>&1 >/dev/null & NOTICE=/tmp/watchdog.txt echo "$NAME2 was not running and was started on `$DATE`" > $NOTICE # $MAIL -n -s "watchdog notice" -c $NOTIFYCC $NOTIFY < $NOTICE $RM -f $NOTICE ;; esac exit