Many of my friends are using Flask framework to write a small server,Easy and fast,But also a key to run,Very comfortable。but,If you really want your service to be deployed on the server,Then you will find that there are some homework to do,This article,We mainly talk about how to write the program into a service on the server instead of the terminal in the execution of the command。
If we write a Python web application,Then it needs to be running to listen for HTTP requests,If direct execution terminal
python main.py
It would be too much trouble,You have to keep the terminal open,End of ssh,Program will be over。
at first,I do so:
nohup python -in /home/logcg/main.py > /home/logcg/out.log 2>&1 &
,Nohup This tool allows a program running in the background,So even if you disconnect the ssh,Closed terminal,The program is still running on the server,If you put the command writes
rc.local
,Then it will be able to follow every time the server is started and start,Perfect ...... it?
This solution looks perfect,There are similar, such as screen commands,But think carefully,If the program is implemented unexpectedly crash how to do?
--Unfortunately,no way,The only way is to wait for your customers to tell you:You can not open a website。
In short,Now we can direct your program "into a" system-level services with another tool,Like Nginx,Like so fail2ban,Once the program crashes,Reboot the system will take care of it - unless you manually stop it。
Supervisor
This is a tool to achieve pure Python,It is a lightweight process management tool,Can automatically initiate and monitor the process depending on your configuration,Once the program crashes unexpectedly,It will restart them。
1 2 |
apt install supervisor service supervisor restart |
After starting,exist
/etc/supervisor/conf.d
Write your command configuration directory,The best is a command to a file,Easy to manage,File name is required
.conf
suffix。
such as,I have such a service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[program:logcg] command=gunicorn wsgi:app -c gunicorn.config.py ; 被监控的进程路径 directory=/home/logcg/ ; 执行前要不要先cd到目录$ autostart=true ; 随着supervisord的启动而启动 autorestart=true ; 自动重启。。当然要选上了 startretries=10 ; 启动失败时的最多重试次数 exitcodes=0 ; 正常退出代码 stopsignal=TERM ; 用来杀死进程的信号 默认 TERM,支持 gunicorn graceful restart stopwaitsecs=10 ; 发送SIGKILL前的等待时间 redirect_stderr=true ; 重定向stderr到stdout ; 默认为 false,如果设置为 true,当进程收到 stop 信号时,会自动将该信号发给该进$ stopasgroup=false ; send stop signal to the UNIX process ; 默认为 false,如果设置为 true,当进程收到 kill 信号时,会自动将该信号发给该进$ killasgroup=false ; SIGKILL the UNIX process group (def false) |
Such,I became a Python program called "logcg" service。
At last,
supervisorctl update all
Update configuration information for all services:
1 2 |
➜ supervisor supervisorctl update all logcg: added process group |
of course,You can also use the command
supervisorctl status all
To view the running status of all services,will
all
Replaced by a specific service name,You will be able to query a single service。
This time,Never worry about because of some unexpected small problem caused the program to crash never get up。
Report an error
unix:///var/run/supervisor.sock no such file
If you unfortunately encounter this error after updating the package,Then you are not alone,https://github.com/Supervisor/supervisor/issues/480 of course,
The above problem has been solved in the new version of Supervisor,But if you are constantly updating,Then this problem may not be repaired automatically,The solution is-reinstall。
Direct use the command
apt remove supervisor
then
apt remove supervisor
Can,The reason is that the path used by the startup script is
/usr/bin/supervisord
And our actual command path is
/usr/local/bin/supervisord
,The service information will not be rebuilt when you update,So it always starts the service from the wrong path,So the service failed to start,Naturally report an error
unix:///var/run/supervisor.sock no such file
Got it。
We also have corresponding solutions:
1 2 |
<del>touch /var/run/supervisor.sock supervisord -c /etc/supervisor/supervisord.conf</del> |
If you configured according to this article,Then just copy and paste to execute these two statements.,The first sentence is to manually create this .sock file,For supervisor to read,The second sentence is to let supervisord start with the default configuration,Then it can be used normally。
References
Original article written by LogStudio:R0uter's Blog » Supervisor will use your Python program becomes Services
Reproduced Please keep the source and description link:https://www.logcg.com/archives/3304.html
Bloggers say good,I chose systemd🐒🐒
No, no,I had to write a script added to the list will not ah ......