Note: Version used in this article: 4.0.4 (Mac environment)

In projects, you often need to set up a process as a daemon (resident process). The easy way is to nohup + &, and the more advanced way is to use Supervisor.

The Supervisor is designed for handling the container in two parts: the container container is designed for handling the container.

When the Supervisor is launched, it’s essentially launching a supervisorprocess that is designed to run the supervised process as its own child process, and it can be automatically restarted if the supervised process crashes.

It is a command-line management tool that is designed to run stop, start and restart commands and is designed to run small processes.

The installation

# Ubuntu
apt install supervisor

# Pip
pip install supervisor

# Mac
brew install supervisor
Copy the code

Start the supervisord

#Mac (Check your own boot mode using Brew Info Supervisor)
supervisord -c /usr/local/etc/supervisord.ini

#Ubuntu host machine
systemctl start supervisor

#Ubuntu Docker container
sudo /etc/init.d/supervisor start
Copy the code

Start the supervisorctl

The container is designed to communicate with the container in two different ways: a local socket connection and an HTTP connection.

The container is designed to handle the HTTP connection by default, so when you run the container with the HTTP configuration disabled, it will display the following error:

$ supervisorctl
http://localhost:9001 refused connection
supervisor> 

Copy the code

Enable HTTP configuration (note that HTTP traffic is not encrypted)

[inet_http_server] ; Inet (TCP) server disabled by default port=127.0.0.1:9001; ip_address:port specifier, *:port for all ifaceCopy the code

Restart the container process

supervisord -c /usr/local/etc/supervisord.ini

If you do not want to enable HTTP configuration, you need to specify the configuration file each time

supervisorctl -c /usr/local/etc/supervisord.ini

Check the version

$ supervisord -v

4.0.4

The configuration file

Master profile

1. For Mac system, the main configuration file located in/usr/local/etc/supervisord. Ini

2. For Ubuntu system, the main configuration file is located in the/etc/supervisor/supervisord. Conf

3. For earlier versions, you may need to manually generate a configuration file

echo_supervisord_conf > /etc/supervisord.conf

Child process configuration file

The path of the child configuration file can be found in the main configuration file. My path is as follows:

[include]
files = /usr/local/etc/supervisor.d/*.ini
Copy the code

Child process configuration file writing

To better illustrate, let’s write a Shell script that periodically prints the current date

#/bin/bash

while true; do
    echo now: `date "+%Y-%m-%d %H:%M:%S"`
    sleep 2
done
Copy the code

The configuration file is as follows:

; Comments use semicolons [program:printime]; Webhook is the process name/service name; Command =bash /home/xxx/print_time.sh; Automatic restart AutoStart =true Autorestart =true user= XXX umask=022; Log file stdout_logfile=/home/ XXX /print_time.log; Redirects error logs to stdout redirect_stderr=true; Loglevel loglevel=infoCopy the code

Update the configuration and start the service

$ supervisorctl update
printime: added process group

$ supervisorctl 
printime             RUNNING   pid 88295, uptime 0:00:06

Copy the code

Reread, Reload, restart, update

Restart: The process is restarted without re-reading the configuration file. Therefore, the service does not use the new configuration

Reread: only the configuration file is updated, but the process is not restarted. Therefore, the service does not use the new configuration

Update: Updates the configuration file and restarts the process that has updated the configuration file. This is equivalent to reread + restart, so the service uses the new configuration

Reload: it is designed to reload the configuration file for all the servers it is running on, and it is designed to reload all the servers it is running on.

Reference: www.onurguzel.com/supervisord… supervisord.org/runnin g.html?highlight=reread

Resolve that the Supervisor cannot read environment variables

When you start a process with Supervisor, you will notice that the process cannot read the system environment variables because the supervisor is started with /etc/init.d, so the system environment variables are not loaded at all.

The workaround is to specify environment variables in configuration files, for example:

[program:xxx] ; You can specify environment variables as follows: export SSH_KEY= XXX; How to specify environment variables using supervisor environment=SSH_KEY= XXXCopy the code

Link: www.v2ex.com/t/400535 www.jianshu.com/p/9f81b42fe…