ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

如何给脚本写一个守护进程?

2021-03-08 22:07:01  阅读:165  来源: 互联网

标签:脚本 __ daemon self start mysql 进程 path 守护


在我们日常运维中,写脚本监控一个进程是比较常见的操作,比如我要监控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来启动其它所有应用程序,就不用一个一个应用程序启动了,而且还能起到实时监控的作用,很方便吧,这篇就到这里


标签:脚本,__,daemon,self,start,mysql,进程,path,守护
来源: https://blog.51cto.com/15060540/2651696

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有