preface

For most programmers, the main job is coding and some simple middleware installation, resulting in many people are unfamiliar with “operations” related work. For example, when we have our own server, we may run our own blog programs, mysql, nginx, etc. More and more when the program is not a unified entrance management start-stop, also may have some special reasons cause the program to kill off, at that time and don’t related to the monitoring program or script (too much trouble to install, don’t want to charging machine configuration), so only when we visit our program found on the server when it is to find the reason.

These conditions are more troublesome for us, then this needs a “magic” to free our hands, dang dang dang!! The Supervisor is coming.

The body of the

The Supervisor is introduced

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. It starts the managed processes as child processes of the Supervisor by fork/exec, which simply writes the path to the executable file of the managed process in the Supervisor configuration file. Also when the child process hangs up, the parent process can accurately obtain the information of the child process hangs up, can choose whether to start and alarm.

The supervisor is installed

Simple and crude

yum install supervisor -y

Supervisor Configuration Description

For the supervisor installed in this format, the configuration file directory is in /etc/supervisor.conf (main configuration file, which we’ll cover in detail below) /etc/supervisor.d/ (the default sub-process configuration file, which is the one we need to configure according to the program).


The basic configuration items are described in the supervisord.conf container. Due to its large number of parameters, it is only posted with some common configuration items, details can be seen on the official website. Warm tips “;” The symbol indicates that the line configuration is commented.

[unix_http_server] file=/home/supervisor/supervisor.sock ; It is the path to the socket file it is handling. chmod=0700 ; The default socket file permission is 0700; chown=nobody:nogroup ; Socket file owner [inet_http_server]; Provides web management and background management configurations port=0.0.0.0:9001. The IP address and port number of the web management background must be considered for security when binding to external networks. username=root ; User name and password for logging in to the Web management background. password=root [supervisord] logfile=/var/log/supervisord.log ; Log files, default in$CWD/supervisord.log logfile_maxbytes=50MB ; If the log size exceeds the limit, new files will be generated. 0: no limit logfile_backups=10. The default value is 10. 0 indicates no log backup loglevel=info. Log level pidfile = / home/supervisor/supervisord pid; supervisord pidfile; default supervisord.pid ; The pid file nodaemon =false; Whether to start in the foreground or background by defaultfalseminfds=1024 ; The minimum value of the file descriptor that can be opened minprocs=200; Can open the process of the minimum [supervisorctl] serverurl = Unix: / / / home/supervisor/supervisor. The sock. The socket is connected to the supervisorord, the path is the same as the unix_http_server->file configuration. Specifies to configure multiple configuration files under the current directory supervisor.d folderCopy the code
Prepare test items

Here I packaged a simple spring-boot program and stored it under “/opt/project/”.

In this application we simply define a REST interface for demonstration purposes.

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(a){
        return "hello world"; }}Copy the code

If we don’t use Supervisor, we usually start the program with a command

nohup java -jar springboot-hello-sample.jar &

This is to start the JAR in the background, and the output for running the program will be in nohup.out, so we won’t go over it, so let’s look at the way we switch to supervisor, how we configure the project, how we manage it.

Define the Supervisor process configuration file

[include]->files supervisor will load the supervisor. D/config file that ends with conf, and create a new sboot.conf file under that directory.

[program:sboot] ; [program: XXX] directory = /opt/project; Program directorycommand= java -jar springboot-hello-sample.jar ; Program start command autostart=true; Is the container running after the launch of the autorestart=true; Automatic restart after program exit, optional value: [unexpected,true.false], the default value is unexpected, indicating that the process will be restarted after it is killed unexpectedlytrue; Whether to send the stop signal to the process group when the process is killed, including the subprocess killasGroup =true; Send to a process groupkillSignal, including the child process stdout_logfile=/var/log/sboot/supervisor.log; The program log output files, directories need to be manually created stdout_logFILe_maxbytes = 50MB; Log size STdout_logFILe_backups = 100; Number of backupCopy the code

As you can see, the Supervisor already configures the program name, startup path, and so on in the configuration file. In this way, the Supervisor has complete control over the life and death of the program. And then we execute

service supervisord restart

Restart supervisord.

Observe effect

Browser: Server IP address :9001 (the port on the Web management page is configured in the configuration file.)

As shown in the figure, we can observe that the Springboot program is in the running state, and the PID is 27517. We can click Tail -f pull to observe the output log, which is similar to the function of “Tail -f” in the server directly.

In order to facilitate the delay, we did not add authentication. If you are in the public network environment, you need to open the user name and password authentication comment in the configuration file. Otherwise others sweep out your background management page can control the program at will.

In addition to web management pages, there are also some simple commands to master.

Entering the container on the command-line is the supervisorctl container that displays the currently configured project.

[root@wangzh supervisor.d]# supervisorctl 
sboot                            RUNNING   pid 27517, uptime 0:18:04
supervisor> 

Copy the code

And then you can execute

Start /stop/restart sboot controls the start and stop of a projectCopy the code
supervisorctl update Update the configuration file
supervisorctl reload Restart the configured program
supervisorctl stop all Stop all admin processes
Copy the code

conclusion

With a few simple configurations, we can manage our programs in a unified manner and automatically restart the process when it dies unexpectedly. These tasks are handed over to the Supervisor, and we can “do whatever we want” as long as we master a few simple commands.

Supervisor official website: www.supervisord.org/