在我们日常运维中,写脚本监控一个进程是比较常见的操作,比如我要监控mysql进程是否消失,如果消失就重启mysql,用下面这段代码就可以实现:
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/bin/sh Date=` date '+%c'` while : do if ! ps aux | grep -w mysqld | grep -v grep >/dev/null 2>&1 then /etc/init.d/mysqld start echo $Date mysqld was reboot >>/var/log/reboot_mysql.log fi done |
本篇这是以mysql为例子,但实际中如果是监控的脚本出了问题,报警没发出来,那就比较尴尬了,所以为保证我们的检查脚本能实时运行,我们需要一个进程来守护这个脚本,这就是我们今天要说的主题,如何给脚本写一个daemon,我们先上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#!/usr/bin/python import subprocess from daemon import runner cmd = "/root/demo_script/restart_mysql.sh" class App(): def __init__(self): self.stdin_path = '/dev/null' self.stdout_path = '/dev/tty' self.stderr_path = '/dev/tty' self.pidfile_path = '/tmp/hello.pid' self.pidfile_timeout = 5 def start_subprocess(self): return subprocess.Popen(cmd, shell=True) def run(self): p = self.start_subprocess() while True: res = p.poll() if res is not None: p = self.start_subprocess() if __name__ == '__main__': app = App() daemon_runner = runner.DaemonRunner(app) daemon_runner.do_action() |
脚本比较简单,没什么特别的逻辑,关于daemon这个模块如何使用,我这里给出官方的解释,注意哟,是英文的,我就不翻译了,如果有生词就查查字典,就当多学几个了单词吧。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
__init__(self, app) | Set up the parameters of a new runner. | | The `app` argument must have the following attributes: | | * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem | paths to open and replace the existing `sys.stdin`, | `sys.stdout`, `sys.stderr`. | | * `pidfile_path`: Absolute filesystem path to a file that | will be used as the PID file for the daemon. If | ``None``, no PID file will be used. | | * `pidfile_timeout`: Used as the default acquisition | timeout value supplied to the runner's PID lock file. | | * `run`: Callable that will be invoked when the daemon is | started. | | do_action(self) | Perform the requested action. | | parse_args(self, argv=None) | Parse command-line arguments. |
这样就完成了,守护进程的启动比较高大上,输入以上代码后,可以直接在终端输入:
#python monitor.py start
当然还有stop,restart等参数。
这里我介绍的是其中一个应用场景,实际中可以灵活运用,比如1台服务器上启动的程序过多,环境配置比较复杂,就可以先启动daemon进程,然后通过daemon来启动其它所有应用程序,就不用一个一个应用程序启动了,这篇就到这里,有问题可以给我留言。