Python 后台脚本

标签 python linux

我有一个在 Linux 上运行的简单 Python 脚本,它有一个在启动时运行的“while True:”循环。我需要它不断运行。我目前只是在启动时将 & 添加到 linux 命令行的末尾。有没有一种简单的方法可以让 if fork 而不是在我运行它时也使用 & ?

import socket
import sys
from datetime import datetime
import os


while True:
   print "hi", "there"
   sleep(100000)
   #do stuff

我主要想从 tty 中分离我的脚本,这样用户就可以退出 tty 并继续运行。

最佳答案

有 python 守护程序库,https://pypi.python.org/pypi/python-daemon/ 但这需要更多的麻烦。最好的办法是使用 cron 作业来实现此功能。

如果你尝试 fork 它,this answer将解释如何做到这一点。

关于Python 后台脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45084812/

相关文章:

python - 推文错误: Failed to parse JSON payload:

python - 如何更改 reportlab.pdfgen 中的文本/字体颜色

python - Python中给定月份的季度的第一个月

python - Django、RabbitMQ 和 Celery - 为什么在我更新开发中的 Django 代码后 Celery 运行我的任务的旧版本?

linux - 如何练习编写真正的 Linux 设备驱动程序?

python - Linux。连接到 Oracle。 cx_Oracle.DatabaseError : DPI-1047 Cannot locate a 64-bit Oracle Client library: "libclntsh. 所以

python - Google App Engine - NDB - 在多条记录上设置属性值

linux - 在Raspberry Pi上尝试Wireguard失败,出现 “RTNETLINK answers: Operation not supported”

linux - 如何在 linux 的目录和子目录中找到在特定单词下方有特定单词的文件/(文件密码)

linux - 仅当文件不在文件中时才向文件添加行