导读 在我们日常运维中,写脚本监控一个进程是比较常见的操作,比如我要监控mysql进程是否消失,如果消失就重启mysql。

用下面这段代码就可以实现:

#!/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进程,我们先上代码:

#!/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这个模块如何使用,我这里给出一段官方的解释,写的非常明白,注意哟,是英文的,在这我就不翻译了,如果不理解就查查字典,就当多学几个单词了吧。

__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来启动其它所有应用程序,就不用一个一个应用程序启动了,而且还能起到实时监控的作用,很方便吧,这篇就到这里,有问题可以给我留言。

原文来自:http://www.yunweipai.com/7701.html

本文地址:https://www.linuxprobe.com/shell-linux-good.html编辑:向金平,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/