Use supervisor to convert a command-line process into a background daemon process

Introduction

When we run a program in the background on the server, we often use nohup command &the program to run in the background, so that the program is still running after we close the terminal, but once the program hangs up, the program will exit directly, affecting business. The supervisor can convert the above-mentioned command-line program into a daemon process, and restart the process when the program exits abnormally.

installation method

sudo apt install supervisor

How to use (take ubuntu20.04 as an example)

We can /etc/supervisorfind supervisord.confand view the contents of the file:

[include]
files = /etc/supervisor/conf.d/*.conf

It means that the supervisor includes all configuration files in this directory.

Instructions

  1. Create myapp.conf, (modified according to the actual situation) the content is:
[program:myapp]  ;程序名称
user=ubuntu  ;执行程序的用户
command=/path/to/myapp ;执行的命令
directory=/path/to/; 命令执行的目录
stopsignal=TERM  ;重启时发送的信号
autostart=true  
autorestart=true  ;是否自动重启
stdout_logfile=/var/log/myapp.log  ;标准输出日志位置
stderr_logfile=/var/log/myapp.log  ;标准错误日志位置
  1. After creating the configuration file, update it
sudo supervisorctl update
  1. Common commands
supervisorctl status       # 查看所有任务状态
supervisorctl shutdown     # 关闭所有任务
supervisorctl start myapp  # 启动任务
supervisorctl stop myapp   # 关闭任务
supervisorctl reload       # 重启supervisor

Reference: https://www.liwenzhou.com/posts/Go/deploy/

Guess you like

Origin blog.csdn.net/qq_41575489/article/details/132171363