我写了一个Python脚本,检查一个特定的电子邮件地址,并将新的电子邮件传递给一个外部程序。如何让这个脚本全天候执行,比如在Linux中将其转换为守护进程或服务。我是否还需要一个在程序中永不结束的循环,或者可以通过多次重新执行代码来完成?
当前回答
如果你正在使用终端(ssh或其他东西),并且你想在退出终端后保持长时间的脚本工作,你可以尝试这样做:
屏幕
Apt-get安装屏幕
在里面创建一个虚拟终端(即abc): screen -dmS abc
现在我们连接到abc: screen -r abc
因此,现在我们可以运行python脚本:python keep_sending_mail .py
从现在开始,你可以直接关闭你的终端,但是,python脚本将继续运行而不是被关闭
因为这个keep_sending_mail .py的PID是虚拟屏幕的子进程,而不是 终端(ssh)
如果你想检查你的脚本运行状态,你可以再次使用screen -r abc
其他回答
你应该使用python-daemon库,它会处理所有的事情。
从PyPI:库实现一个行为良好的Unix守护进程。
一个简单且受支持的版本是Daemonize。
从Python包索引(PyPI)安装它:
$ pip install daemonize
然后用like:
...
import os, sys
from daemonize import Daemonize
...
def main()
# your code here
if __name__ == '__main__':
myname=os.path.basename(sys.argv[0])
pidfile='/tmp/%s' % myname # any name
daemon = Daemonize(app=myname,pid=pidfile, action=main)
daemon.start()
如果你正在使用终端(ssh或其他东西),并且你想在退出终端后保持长时间的脚本工作,你可以尝试这样做:
屏幕
Apt-get安装屏幕
在里面创建一个虚拟终端(即abc): screen -dmS abc
现在我们连接到abc: screen -r abc
因此,现在我们可以运行python脚本:python keep_sending_mail .py
从现在开始,你可以直接关闭你的终端,但是,python脚本将继续运行而不是被关闭
因为这个keep_sending_mail .py的PID是虚拟屏幕的子进程,而不是 终端(ssh)
如果你想检查你的脚本运行状态,你可以再次使用screen -r abc
Ubuntu有一个非常简单的方法来管理服务。 对于python来说,不同之处在于所有依赖项(包)都必须在运行主文件的同一目录中。
我只是设法创建了这样一个服务,为我的客户提供天气信息。 步骤:
Create your python application project as you normally do. Install all dependencies locally like: sudo pip3 install package_name -t . Create your command line variables and handle them in code (if you need any) Create the service file. Something (minimalist) like: [Unit] Description=1Droid Weather meddleware provider [Service] Restart=always User=root WorkingDirectory=/home/ubuntu/weather ExecStart=/usr/bin/python3 /home/ubuntu/weather/main.py httpport=9570 provider=OWMap [Install] WantedBy=multi-user.target Save the file as myweather.service (for example) Make sure that your app runs if started in the current directory python3 main.py httpport=9570 provider=OWMap The service file produced above and named myweather.service (important to have the extension .service) will be treated by the system as the name of your service. That is the name that you will use to interact with your service. Copy the service file: sudo cp myweather.service /lib/systemd/system/myweather.service Refresh demon registry: sudo systemctl daemon-reload Stop the service (if it was running) sudo service myweather stop Start the service: sudo service myweather start Check the status (log file with where your print statements go): tail -f /var/log/syslog Or check the status with: sudo service myweather status Back to the start with another iteration if needed
此服务现在正在运行,即使您退出也不会受到影响。 如果主机关闭并重新启动,则此服务将重新启动…
你可以使用fork()将你的脚本从tty中分离出来,并让它继续运行,如下所示:
import os, sys
fpid = os.fork()
if fpid!=0:
# Running as daemon now. PID is fpid
sys.exit(0)
当然你还需要实现一个无限循环,比如
while 1:
do_your_check()
sleep(5)
希望这能让你开始。
推荐文章
- 如何从psycopg2游标获得列名列表?
- Python中dict对象的联合
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- 如何有效地比较两个无序列表(不是集合)?
- Windows和Linux上的c++编译:ifdef开关
- 如何在交互式Python中查看整个命令历史?
- 如何显示有两个小数点后的浮点数?
- 如何用OpenCV2.0和Python2.6调整图像大小
- 在每个列表元素上调用int()函数?
- Linux: kill后台任务
- 当使用代码存储库时,如何引用资源的相对路径
- 如何修改Linux系统中打开文件的数量限制?
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列