Supervisor will use your Python program becomes Services

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。

After starting,in /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:

Such,I became a Python program called "logcg" service。

At last, supervisorctl update all Update configuration information for all services:

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 The。

 

We also have corresponding solutions:

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 Gerber drop-off: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

About the Author

R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published.