首页 > 解决方案 > 在网络服务器上运行 python 脚本(后台)

问题描述

我目前正在创建一个制作“统计信息”的脚本,它必须不断运行,我想将它上传到我的网络服务器(它有 cPanel)上运行,而不是在我的计算机上运行。我希望它在后台运行,所以我不需要打开任何网页或其他任何东西。那可能吗?

标签: pythonwebservercpanel

解决方案


主管是一个很好的方法来做到这一点。

http://supervisord.org/ 这允许您在启动时像服务一样运行脚本。还允许您指定命令行选项并指定日志记录。有自动重启的选项。

示例配置:

[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

如果您希望在 Web 服务器的上下文中运行它,您可能还希望使用 Django,它是一个用于网站/应用程序的 Python Web 框架。然后你可以使用 Celery 来安排你的 Python 函数。 https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html


推荐阅读