我写了一个Python脚本,检查一个特定的电子邮件地址,并将新的电子邮件传递给一个外部程序。如何让这个脚本全天候执行,比如在Linux中将其转换为守护进程或服务。我是否还需要一个在程序中永不结束的循环,或者可以通过多次重新执行代码来完成?


当前回答

使用你的系统提供的任何服务管理器——例如在Ubuntu下使用upstart。这将为您处理所有细节,如启动时启动,崩溃时重新启动等。

其他回答

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

此服务现在正在运行,即使您退出也不会受到影响。 如果主机关闭并重新启动,则此服务将重新启动…

假设您真的希望您的循环作为后台服务全天候运行

对于一个不需要向代码中注入库的解决方案,你可以简单地创建一个服务模板,因为你使用的是linux:

[Unit]
Description = <Your service description here>
After = network.target # Assuming you want to start after network interfaces are made available
 
[Service]
Type = simple
ExecStart = python <Path of the script you want to run>
User = # User to run the script as
Group = # Group to run the script as
Restart = on-failure # Restart when there are errors
SyslogIdentifier = <Name of logs for the service>
RestartSec = 5
TimeoutStartSec = infinity
 
[Install]
WantedBy = multi-user.target # Make it accessible to other users

将该文件放在您的守护进程服务文件夹(通常是/etc/systemd/system/)中,*. .使用以下systemctl命令安装它(可能需要sudo权限):

systemctl enable <service file name without .service extension>

systemctl daemon-reload

systemctl start <service file name without .service extension>

然后你可以使用命令检查你的服务是否在运行:

systemctl | grep running

你有两个选择。

Make a proper cron job that calls your script. Cron is a common name for a GNU/Linux daemon that periodically launches scripts according to a schedule you set. You add your script into a crontab or place a symlink to it into a special directory and the daemon handles the job of launching it in the background. You can read more at Wikipedia. There is a variety of different cron daemons, but your GNU/Linux system should have it already installed. Use some kind of python approach (a library, for example) for your script to be able to daemonize itself. Yes, it will require a simple event loop (where your events are timer triggering, possibly, provided by sleep function).

我不建议你选择2。,因为您实际上是在重复cron功能。Linux系统范例是让多个简单工具交互并解决您的问题。除非有其他原因需要创建守护进程(除了定期触发之外),否则选择其他方法。

此外,如果你使用daemonize进行循环并且发生了崩溃,那么没有人会在那之后检查邮件(正如Ivan Nevostruev在回答的评论中指出的那样)。而如果脚本作为cron作业添加,它将再次触发。

你应该使用python-daemon库,它会处理所有的事情。

从PyPI:库实现一个行为良好的Unix守护进程。

在linux下使用$nohup命令如何?

我用它在Bluehost服务器上运行命令。

如果我错了,请指教。