How to write a daemon to a script
A dream of Baozheng 2021-06-04 10:26:46

In our daily operation and maintenance , Writing scripts to monitor a process is a common operation , For example, I want to monitor mysql Whether the process disappears , If it disappears, restart mysql.

You can do it with the following code :

#!/bin/shDate=` date ‘+%c’`while :doif ! ps aux | grep -w mysqld | grep -v grep >/dev/null 2>&1then/etc/init.d/mysqld startecho $Date mysqld was reboot >>/var/log/reboot_mysql.logfidone

But if something goes wrong with the monitoring script itself , We can't start the program as we asked , Here, this is based on mysql As an example , But in practice, if there is something wrong with the script responsible for the alarm , The police didn't come out , That would be awkward , So in order to make sure that our checking script can run in real time , We sometimes need to write a daemon ( It's not just scripts, of course , Any program in the system can be started by the daemons ), That's what we're going to talk about today , How to write a script daemon process , Let's start with the code :

#!/usr/bin/pythonimport subprocessfrom daemon import runnercmd = “/root/demo_script/”class App():def __init__(self):self.stdin_path = ‘/dev/null’self.stdout_path = ‘/dev/tty’self.stderr_path = ‘/dev/tty’self.pidfile_path = ‘/tmp/’self.pidfile_timeout = 5def 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()

The script is simple , There's no particular logic , About daemon How to use this module , I'll give you an official explanation here , It's very clear , Pay attention , Is English , I won't translate here , If you don't understand, look it up in the dictionary , Just learn a few more words .

__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.

That's it , The startup of the daemons is relatively high , After entering the above code , It can be input directly at the terminal :

#python start

Of course, stop,restart Equal parameter .

Here I introduce one of the application scenarios , It can be used flexibly in practice , such as 1 Too many programs started on one server , The environment configuration is complex , You can start it first daemon process , And then through daemon To start all other applications , You don't have to start one application at a time , And it can also play the role of real-time monitoring , It's convenient , That's all for this article , If you have any questions, please leave me a message .

The great God teaches you how to write a daemon for a script | 《Linux It's time to learn 》 (
Please bring the original link to reprint ,thank
Similar articles