What is a daemon

In Linux or Unix, daemons are special processes that run in the background, independent of the control terminal, and periodically perform certain tasks or wait to process certain events. In Linux, the interface for each system to communicate with users is called terminal, and every process running from this terminal will be attached to this terminal, which is called the control terminal of these processes. When the control terminal is shut down, the corresponding process will be shut down automatically. However, daemons can break through this limitation. They are detached from the terminal and run in the background, and they are detached from the terminal in order to prevent information from being displayed on any terminal while the process is running, and the process is not interrupted by any terminal information generated by any terminal. It runs from the time it is executed until the entire system shuts down.

The role of the daemon

Daemons are usually started when the system boots up and terminated when the system shuts down. Linux has many daemons. Most services are implemented through daemons, and daemons can perform many system tasks, such as job planning process crond and printing process LQD (the d at the end stands for Daemon).

The Supervisor is introduced

There are several ways to run a daemon on Linux, such as Nohup, Screen, etc., but to run reliably in the background as a service, you need to make it a daemon, preferably one that can monitor the status of the process and automatically restart when it ends unexpectedly. Supervisor is a universal process manager program developed in Python. It changes a common command line process into a background daemon, monitors the process status, and automatically restarts when an abnormal exit occurs.

The Supervisor is installed

The installation

> yum install python-setuptools
> easy_install supervisor
Copy the code

or

> wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.0b1.tar.gz
> tar -zxvf superviosr-3.0b1.tar.gz
> cdSuperviosr-3.0b1 > Python setup.py installCopy the code

Create the Supervisor configuration file

  1. runecho_supervisord_confCommand to generate a configuration file templatesupervisord.confAnd on the/etc/directory
echo_supervisord_conf > /etc/supervisord.conf
Copy the code
  1. Edit supervisord. Conf, cancel the last include annotations, and modify the corresponding folder/usr/local/supervisor/conf / *. Ini can deposit user-defined task profile;

  2. In the/usr/local/supervisor/conf new custom task profile

> vim mytask.ini
# File contents
[program:myTaskName]
command=php -m
directory=/home
autorestart=true
autostart=true
stderr_logfile=/home/logs/supervisor/myTaskName.err.log
stdout_logfile=/home/logs/supervisor/myTaskName.out.log
user=root
startsecs=1

Copy the code
  1. Management of the supervisor
# start
supervisord -c /etc/supervisord.conf
# stop
supervisorctl shutdown
Reload the configuration
supervisorctl reload
Copy the code
  1. Management process
# Update task
supervisorctl update
Unlock all missions
supervisorctl start all
# Stop all missions
supervisorctl stop all
Restart all tasks
supervisorctl restart all
Check the task status
supervisorctl status all
Copy the code

Boot from the rev.

New file supervisord.service

#supervisord.service

[Unit] 
Description=Supervisor daemon

[Service] 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf 
ExecStop=/usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s

[Install] 
WantedBy=multi-user.target
Copy the code

Copy the file to /usr/lib/systemd/system/

cp supervisord.service /usr/lib/systemd/system/
Copy the code

Start the service

systemctl enable supervisord
Copy the code

Verify that the boot is enabled

systemctl is-enabled supervisord
Copy the code

reference

  • www.jianshu.com/p/e606f486a…
  • www.jianshu.com/p/8594c1afe…
  • www.jianshu.com/p/03619bf7d…
  • supervisord.org