简体   繁体   English

在网络服务器上运行 python 脚本(后台)

[英]Run python script on webserver (background)

I am currently creating a script that makes "statistics" and it has to run continually, and I would like to upload it on my webserver (it has cPanel) to run on it, and not on my computer.我目前正在创建一个生成“统计”的脚本,它必须持续运行,我想将它上传到我的网络服务器(它有 cPanel)上运行,而不是在我的电脑上运行。 I want it to run in backround so I don't need to open any webpage or anyhting else.我希望它在后台运行,所以我不需要打开任何网页或其他任何内容。 Is that possible?那可能吗?

Supervisor is a good way to do this.主管是一个很好的方法来做到这一点。

http://supervisord.org/ This allows you to run the script at boot just like a service. http://supervisord.org/这允许您像服务一样在启动时运行脚本。 Also allows you to specify command line options and specify logging.还允许您指定命令行选项并指定日志记录。 Has options for auto restart.有自动重启选项。

Example config:示例配置:

[program:test]
command=/usr/bin/python /home/ubuntu/test.py
directory=/home/ubuntu
autostart=true
autorestart=true
startretries=3
stderr_logfile=/home/ubuntu/test.err.log
stdout_logfile=/home/ubuntu/test.out.log
user=ubuntu

If you are looking to run it in the context of the web server, you might also looking to to Django, which is a Python web framework for web sites / applications.如果您希望在 Web 服务器的上下文中运行它,您可能还希望使用 Django,它是一个用于网站/应用程序的 Python Web 框架。 Then you could use Celery to schedule your Python functions.然后你可以使用 Celery 来调度你的 Python 函数。 https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM