Use a supervisor to manage processes.
Supervisor (http://supervisord.org) is a process management tool written in Python that can be easily used to start, restart, and close processes (not just Python processes ). In addition to controlling a single process, you can also start and close multiple processes at the same time. For example, unfortunately, a server problem causes all applications to be killed, in this case, you can use the supervisor to start all applications at the same time instead of running commands one by one.
Install
Supervisor can run on Linux and Mac OS X. As mentioned above, the supervisor is written in Python, so it is easy to install. You can directly use pip:
sudo pip install supervisor
You can also use apt-get to install Ubuntu.
Supervisord Configuration
Supervisor is quite powerful and provides many functions, but we may only need to use a small part of it. After the installation is complete, you can write a configuration file to meet your needs. For convenience, we divide the configuration into two parts: supervisord (supervisor is a C/S model program, this is the server side, corresponding to the client side: supervisorctl) and the application (that is, the program we want to manage ).
First, let's look at the supervisord configuration file. After installing the supervisor, you can runecho_supervisord_confThe default configuration item output by the command can also be redirected to a configuration file:
echo_supervisord_conf > /etc/supervisord.conf
To remove most of the comments and irrelevant parts, let's take a look at these configurations:
[Unix_http_server] file =/tmp/supervisor. sock; UNIX socket file, which will be used by supervisorctl; chmod = 0700; socket file mode, default value: 0700; chown = nobody: nogroup; socket file owner, format: uid: gid; [inet_http_server]; HTTP server, which provides the web management interface; port = 127.0.0.1: 9001; IP address and port running on the Web management background. If it is open to the public network, pay attention to security; username = user; username used to log on to the management background; password = 123; logon password [supervisord] logfile =/tmp/supervisord. log; log file. The default value is $ CWD/supervisord. loglogfile_maxbytes = 50 MB; log file size, rotate if it exceeds the limit; 50 MB logfile_backups = 10 by default; log file retention backup quantity: 10 by default loglevel = info; Log Level: info by default; others: debug, warn, tracepidfile =/tmp/supervisord. pid; pid file nodaemon = false; whether to start at the front end; default value: false, that is, start minfds = 1024 in daemon mode; minimum value of the file descriptor that can be opened, the default value is 1024 minprocs = 200. the minimum number of processes that can be opened is 200 by default. the below section must remain in the config file for RPC; (supervisorctl/web interface) to work, additional interfaces may be; added by defining them in separate rpcinterface: sections [rpcinterface: supervisor] supervisor. rpcinterface_factory = supervisor. rpcinterface: make_main_rpcinterface [supervisorctl] serverurl = unix: // tmp/supervisor. sock; Connect to supervisord through UNIX socket, and the path is consistent with the file in unix_http_server; serverurl = http: // 127.0.0.1: 9001; Connect to supervisord through HTTP; include other configuration files [include] files = relative/directory /*. ini; it can be *. conf or *. ini
We save the above configuration to/etc/supervisord. conf (or any other files with access permissions), and then start supervisord (specify the path of the configuration file through the-c option. If not specified, the configuration file will be searched in this order: $ CWD/supervisord. conf, $ CWD/etc/supervisord. conf,/etc/supervisord. conf ):
supervisord -c /etc/supervisord.conf
Check whether supervisord is running:
ps aux | grep supervisord
Program Configuration
We have run supervisrod above. Now we can add the configuration file of the process we want to manage. All configuration items can be written to the supervisord. conf file, but this is not recommended. Instead, different programs (groups) can be written to different configuration files through the include method.
For example, create a directory/etc/supervisor/to store these configuration files. Modify the include configuration in/etc/supervisord. conf:
[include]files = /etc/supervisor/*.conf
Suppose there is a user center system written in Python and Flask framework, named usercenter, with gunicorn (http://gunicorn.org/) as web server. The project code is located in/home/leon/projects/usercenterThe gunicorn configuration file isgunicorn.py, WSGI callable is the app attribute in wsgi. py. Therefore, the command line may start like this:
cd /home/leon/projects/usercentergunicorn -c gunicorn.py wsgi:app
Write a configuration file to manage the process (Note: When supervisord is used for management, set the daemon option of gunicorn to False.):
[Program: usercenter] directory =/home/leon/projects/usercenter; program startup directory command = gunicorn-c gunicorn. py wsgi: app; Startup command. It can be seen that autostart = true is the same as the command manually started on the command line; startsecs is also automatically started at supervisord startup = 5; if the program does not exit after 5 seconds of startup, it is deemed that autorestart = true has been started properly; startretries is automatically restarted after the program exits abnormally = 3; the number of automatic retries after startup failure, the default value is 3 user = leon. Which user can start redirect_stderr = true? stderr is redirected to stdout. The default value is falsestdout_logfile_maxbytes = 20 MB. The default value is 50MBstdout_logfile_backups; stdout log file backup count; stdout log file, note that it cannot be started normally when the specified directory does not exist, so you need to manually create a directory (supervisord will automatically create a log file) stdout_logfile =/data/logs/usercenter_stdout.log; you can use environment to add required environment variables. A common usage is to modify PYTHONPATH; environment = PYTHONPATH = $ PYTHONPATH: /path/to/somewhere
A configuration file requires at least one[program:x]To tell supervisord to manage the process.[program:x]In the syntaxxIndicates the program name, which is displayed on the client (supervisorctl or web Interface). In supervisorctl, start, restart, and stop operations are performed on the program.
Configuration example:
[program:api-cache-server]command=python api_cache_server.py -c /root/sxadp-prod-confnumprocs=1directory=/root/api_trigger_script/stdout_logfile=/var/log/api-cache-server/api-cache-server.logstdout_logfile_maxbytes=10MBstdout_logfile_backups=3redirect_stderr=trueautostart=trueautorestart=trueuser=rootstopasgroup=true
Use supervisorctl
Supervisorctl is a command line Client tool of supervisord. You must specify the same configuration file as supervisord at startup. Otherwise, the configuration file will be searched in sequence like supervisord.
supervisorctl -c /etc/supervisord.conf
The above command will go to the shell Interface of supervisorctl, and then you can execute different commands:
> Status # view the program status> stop usercenter # Close the usercenter program> start usercenter # start the usercenter program> restart usercenter # restart the usercenter program> reread # Read the configuration files with updates (added, will not start the newly added program> update # restart the program modified in the configuration file
The above commands have corresponding output. In addition to entering the supervisorctl shell interface, you can also run them directly on the bash terminal:
$ supervisorctl status$ supervisorctl stop usercenter$ supervisorctl start usercenter$ supervisorctl restart usercenter$ supervisorctl reread$ supervisorctl update
Others
In addition to supervisorctl, you can also configure supervisrod to start the web management interface. This web Background uses Basic Auth for identity authentication.
In addition to the control of a single process, you can also configure a group for group management.
Frequently view log files, including supervisord logs and prord log files. Half of the program crash or information that throws an exception will be output to stderr. You can view the corresponding log files to find the problem.
Supervisor has a wealth of features, there are many other configurations, you can get more information in the official documents: http://supervisord.org/index.html
Refer:
Http://liyangliang.me/posts/2015/06/using-supervisor/