在Linux系统中,进程守护是一种常见的管理方式,它可以帮助我们监控和管理进程的运行状态,Supervisor是Linux下的一个进程守护工具,它可以帮助我们启动、停止、重启和管理进程,本文将详细介绍如何在Linux中安装配置和使用Supervisor。
Supervisor简介
Supervisor是一个C/S模式的进程管理工具,它通过fork一个supervisord进程来监听配置文件中的进程管理指令,当指定的进程出现异常时,supervisord会自动处理并重新启动该进程,supervisord还提供了进程日志记录、进程状态监控等功能。
Supervisor的安装
1、使用包管理器安装
在基于Debian的系统(如Ubuntu)中,可以使用apt-get命令安装:
sudo apt-get update sudo apt-get install supervisor
在基于RPM的系统(如CentOS)中,可以使用yum命令安装:
sudo yum install epel-release sudo yum install supervisor
2、从源码编译安装
首先从官方网站下载源码包:https://github.com/supervisor/supervisor/releases
然后解压源码包,进入解压后的目录,执行以下命令进行编译和安装:
tar xzf supervisor-x.y.z.tar.gz cd supervisor-x.y.z make sudo make install
Supervisor的配置
1、创建配置文件
Supervisor的配置文件为supervisord.conf,通常位于/etc/目录下,我们可以使用任何文本编辑器创建和编辑该文件,使用vim编辑器创建一个名为supervisord.conf的文件:
sudo vim /etc/supervisord.conf
2、配置示例
以下是一个简单的supervisord.conf配置文件示例:
[unix_http_server] file=/tmp/supervisor.sock ; (the path to the socket file) chmod=0700 ; sockef file mode (default 0700) [supervisord] logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid) childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) numprocs=1 ; (the number of autostart processes) [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket httpport=127.0.0.1:9001 ; use port 9001 for HTTP server, default is 9001
3、启动Supervisor
配置完成后,可以使用以下命令启动Supervisor:
sudo /usr/bin/supervisord -c /etc/supervisord.conf
Supervisor的使用
1、添加进程管理指令
在配置文件中添加进程管理指令,
[program:myapp] command=/path/to/myapp ; command to start your program autostart=true ; start your program at supervisord startup (default: true) startsecs=10 ; of secs prog must stay up to be considered running (default: 10) autorestart=true ; when prog exits, automatically restart it (default: true) exitcodes=0,2 ; possible exit codes (default: 0,2) ; if prog gives stdio to stdout/stderr, redirect to log file (default is no) ; status check method, empty means use default status check function (default: status) ; directory to store the program's log files (default is <logdir>/<progname>) ; maximum number of seconds that the program will be allowed to run without generating an error message (default: 3600) ; wait that many seconds after the process has started before sending SIGKILL to it if it doesn't exit on its own within <startsecs> seconds (default: 75) ; if set, send a SIGKILL signal to the program if it doesn't exit after <stopwaitsecs> seconds after receiving a SIGTERM (default: false) ; stop program with SIGINT (default: true) ; stop program when receiving SIGTERM, SIGINT and SIGQUIT (default: true) ; stop program when SIGWINCH occurs (default: false) ; how to send SIGKILL to the program: 'SIGKILL' will send SIGKILL to all processes in the process group (default: 'SIGTERM') ; kill only processes belonging to this group, when sending a signal to the master process (default: false) ; whether or not to clear environment from the process (default: false) ; whether or not to set umask in the process (default: false) ; whether or not to create a lock file as PID directory (default: false) ; if set, don't change working directory in the child process (default: false) ; if set, don't chdir to the cwd specified in the config file in the child process (default: false) ; extra arguments to be passed to the program (default: none) ; logging level for syslog messages of this process (default: notice) ; syslog facility for logging messages of this process (default: daemon) ; setsid() call in the child process (default: true) ; whether or not to create a private temporary directory for this process (default: false) ; whether or not to create a private user ID for this process (default: false) ; whether or not to cleanup service connection files after process termination (default: true) ; whether or not to reset standard I/O handles after process termination (default: true) ; whether or not to execute a custom prestart script before starting the process (default: false) ; whether or not to execute a custom poststart script after starting the process (default: false) ; whether or not to execute a custom prestop script before stopping the process (default: false) ; whether or not to execute a custom poststop script after stopping the process (default: false) ; whether or not to execute a custom postrestart script after stopping and re-starting the process (default: false) ; command to execute when killing process (default: kill -TERM {{process_name}}). Note that this is executed with root privileges! ; command to execute when starting process (default: "") ; command to execute when stopping process (default: "kill -TERM {{process_name}}") ; command to execute when resuming process after stoppage (default: "") ; command to execute when attempting to daemonize a process that requires it (default: "") ; name of the xinetd style init script that will be called instead of starting the process directly (default: "") ; additional environment variables you want to set in this process's environment (default: none) ; other valid key-value pairs are passed as-is to the program specified in "command" ; see full documentation at http://supervisord.org/configuration.htmlprogram-x-section-settings for details about these options. ; note that all settings in this section are optional! ; [program:myapp] ; [group:one] ; programs = one two three ; [program:one] ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [program:two] ----------------------------------------------------------------------------------------------------------------
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/337127.html