python - 永远运行 Python 脚本,记录错误并在崩溃时重新启动

标签 python node.js python-2.7 meteor forever

我有一个持续处理新数据并写入 mongodb 的 python 脚本。在脚本中,它是一个 while 循环和一个连续运行代码的 sleep

永久运行 Python 脚本、在错误发生时记录错误并在崩溃时重新启动的推荐方法是什么?

node.js的forever会合适吗?我还在同一台 Ubuntu 服务器上运行 Node/ meteor 。

最佳答案

supervisord 非常适合这种事情。虽然我过去常常使用 cron 作业检查程序是否仍在运行,但 supervisord 在进程内线程中运行所有程序,因此如果您的程序终止,supervisord 将自动重启过程。我不再需要解析 ps 的输出来查看程序是否崩溃。

它有一个简单的声明性配置文件和可配置的日志记录。默认情况下,它会为 your-program-name-stderr.log 创建一个日志文件来自操作系统包管理器(对我来说是 Debian)。

如果你不想配置supervisord的日志记录,你应该看看logging在 python 中,因此您可以控制进入这些文件的内容。

如果您使用的是 debian 衍生版,您应该能够通过以 root 身份执行 apt-get install supervisord 来安装和启动守护进程。

配置文件也非常简单:

[program:myprogram]
command=/path/to/my/program/script 
directory=/path/to/my/program/base
user=myuser
autostart=true
autorestart=true
redirect_stderr=True

supervisorctl 还允许您以交互方式查看您的程序正在做什么,并且可以使用 supervisorctl start myprogram 等启动和停止多个程序

关于python - 永远运行 Python 脚本,记录错误并在崩溃时重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20506654/

相关文章:

python - 是否可以将 python 子进程的输出实时流式传输到网页?

python - PIL 的 Image.show() 带来了*两个*不同的观众

python - tensorflow cifar10_eval.py 错误 :RuntimeError: Attempted to use a closed Session. RuntimeError: Attempted to use a closed Session

mysql - mysql SP 中使用数组参数时出错

python - 获取具有退出状态的单独变量中的 stderr 和 stdout

regex - 如果从 config.cfg 文件中获取,则正则表达式 Pattern 对象将转换为字符串

python pangram 跳过第二个字母

node.js - 为什么无服务器端点 URL 不起作用?

arrays - NodeJS : incorrect result of array. 加入

python - python中的函数调用函数