If you want to resident a process in the background, the first thought is to add ampersand after the command to hide the purpose of the program in the background. Although the process appears to be running in the background, in fact, the process will be killed when the terminal session is closed. This problem is usually solved with the nohup command. The effect of nohup is to ignore the SIGHUP signal, which is sent to all running programs in the session when the session is closed. In short, nohup commands with & can not only run in the background, but are not affected by the session closing.
$ nohup /bin/cat &
Copy the code
The problem is that while it can run in the background and avoid the effects of hanging up, it can’t prevent the resident process from having problems of its own. Once it terminates the process by itself, the golden pair is helpless. So how do I pull up a resident process that has stalled? This is our topic, Supervisor.
The Supervisor is introduced
Supervisor is a special tool for monitoring and managing processes on Unix-like systems. It was released in 2004. Although its name is magnificent, its ambition is not to co-ordinate the processes of the entire operating system, but to be an obedient personal assistant. Heal the wounded and save the dying to ensure the continuous operation of the process.
The Unix-like system is an operating system evolved from the Unix design style. In addition to Windows, most of the systems on the market are UNIx-like systems.
Supervisor is the C/S architecture that is responsible for the container container container container container container container container container container container container container container container Container Container Container Container Container Container Container Container Container The Supervisor’s main function is to launch the configured program, respond to commands sent by the Supervisor, and restart the exiting child process while the Supervisor’s main function is the Supervisor’s client, which provides a series of parameters in the form of a command line to enable the user to send instructions to the Supervisor. Commonly used commands include start, pause, remove, and update.
Supervisor installation and configuration
Installing Supervisor is simple and can be installed directly in the package manager of any major operating system.
$ yum install -y supervisor
Copy the code
The installed Supervisor configuration file is /etc/supervisor.conf by default. If you cannot find the Supervisor configuration file, you can run an official command to generate the Supervisor configuration file. You only need to add a configuration file to this directory to dynamically expand, so supervisor.conf generally doesn’t need to be changed.
Let’s take the simplest cat command, which blocks without arguments and waits for standard input, so it’s a good example of a resident process. Now create a configuration file called cat.ini to /etc/supervisor.d/. The first line defines the name of the program, which is used to identify the action, and the second line defines the command path, which is the underlying command that the program executes.
[program:foo]
command=/bin/cat
Copy the code
When the supervisor is configured, it is shown that the supervisor configuration file is specified through the -c container.
$ supervisord -c /etc/supervisord.conf
Copy the code
If you do not specify a configuration path, it will search the configuration files one by one in the following order:
- $CWD/supervisord.conf
- $CWD/etc/supervisord.conf
- /etc/supervisord.conf
- /etc/supervisor/supervisord.conf
- ../etc/supervisord.conf
- ../supervisord.conf
If you install Supervisor on a Mac OS, you may not be able to find the configuration file in either of these directories. Instead, you can use the official commands to generate the configuration.
$ echo_supervisord_conf > supervisor.conf
Copy the code
The CAT process should be running at this point.
$ ps aux | grep /bin/cat
Copy the code
When the process is killed, the process ID changes, proving that the Supervisor has pulled the CAT up again.
$ sudo kill9 < process ID>
Copy the code
Core Configuration
Command is not the only option in the configuration file.
[program:name]
command=sh /tmp/echo_time.sh
priority=999
numprocs=1
autostart=true
autorestart=true
startsecs=10
startretries=3
exitcodes=0.2
stopsignal=QUIT
stopwaitsecs=10
user=root
log_stdout=true
log_stderr=true
logfile=/tmp/echo_time.log
logfile_maxbytes=1MB
logfile_backups=10
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=20
stdout_logfile=/tmp/echo_time.stdout.log
Copy the code
Here are a few configuration briefs
- Command: Indicates the command to be executed
- Priority: indicates the execution priority. The higher the value, the later the startup and the earlier the shutdown
- Numprocs: indicates the number of processes
- Autostart: whether it is launched in conjunction with the container
- Autorestart: automatically restarts
- Startsecs: indicates the delayed startup time. The default value is 10 seconds
- Startretries: indicates the number of startup retries. The default value is three
- Exitcodes: Restart a program when its exitcode is 0 or 2
- Stopsignal: indicates the stopsignal
- Stopwaitsecs: Delay stop time, how long to stop after receiving the stop command
- User: indicates the user name
Dynamically operating subroutines
To add a new container, simply add a configuration file and execute the container update to create the container that is running on the container without the need to restart the container service. When refused connection is refused, the configuration file cannot be found.
$ supervisorctl update
foo1: added process group
Copy the code
In the same way as delete, remove shuts down the process before removing it from the list.
foo1: stopped
foo1: removed process group
Copy the code
If you need to stop a program individually, you can use the stop command followed by the program name.
$ supervisorctl stop foo
foo: stopped
Copy the code
You can also use the stop all command to stop all processes more violently.
$ supervisorctl stop all
foo: stopped
foo1: stopped
Copy the code
Vice versa, to start the process, simply change stop to start.
$ supervisorctl start all
Copy the code
Connect to a process
$ supervisorctl fg< name > program
Copy the code
Restart the supervisord
$ supervisorctl reload
Copy the code
Dynamically load supervisor.conf
$ supervisorctl reread
Copy the code
View the running status of all processes
$ supervisorctl status
Copy the code
Web UI Operations
The supervisor provides the UI operation mode. You need to remove the comment of inet_http_server from supervisor.conf.
[inet_http_server] ; Inet (TCP) server disabled by default port=127.0.0.1:9001; ip_address:port specifier, *:port for all iface username=user ; default is no username (open server) password=123 ; default is no password (open server)Copy the code
Restart supervisord.
$ supervisorctl reload
Copy the code
Visit http://localhost:9001 to view the Supervisor interface.
My name is Ping Ye. There is an open source project “Go Home” focusing on the growth of Gopher technology.
Thank you for watching, if you feel the article is helpful to you, welcome to pay attention to the public account “Ping ye”, focus on Go language and technology principle.