简体   繁体   English

如何按计划在后台运行我的python脚本?

[英]How can I run my python script in the background on a schedule?

I have a small python script that creates a graph of data pulled from MySQL. 我有一个小的python脚本,可以创建从MySQL中提取的数据图。 I'm trying to figure out a way to run the script in the background all time on a regular basis. 我正试图找出一种在后台运行脚本的方法。 I've tried a number of things: 我尝试过很多东西:

  1. A Cron Job that runs the script 运行脚本的Cron作业
  2. A loop timer 循环计时器
  3. Using the & command to run the script in the background 使用&命令在后台运行脚本

These all have there pluses and minuses: 这些都有优点和缺点:

  1. The Cron Job running more then every half hour seems to eat up more resources then it's worth. 每半个小时运行一次以上的Cron Job似乎占用了更多的资源然后它的价值。
  2. The Loop timer put into the script doesn't actually put the script in the background it just keeps it running. 放入脚本的循环计时器实际上并没有将脚本放在后台,只是让它保持运行。
  3. The Linux & command backgrounds the process but unlike a real Linux service I can't restart/stop it without killing it. Linux&命令介绍了该过程,但与真正的Linux服务不同,我无法在不杀死它的情况下重新启动/停止它。

Can someone point me to a way to get the best out of all of these methods? 有人能指出我的方法来充分利用所有这些方法吗?

Why don't you try to make your script into a proper daemon. 为什么不尝试将脚本变成正确的守护进程。 This link is a good place to start. 这个链接是一个很好的起点。

import os
import subprocess
import time
from daemon import runner

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/your-pid-name.pid'
        self.pidfile_timeout = 5
    def run(self):

        try:
            while True:

                ### PUT YOUR SCRIPT HERE ###

                time.sleep(300)

        except Exception, e:
            raise

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

You can start/stop/restart this script just like any other linux service. 您可以像任何其他Linux服务一样启动/停止/重新启动此脚本。

The cron job is probably a good approach in general, as the shell approach requires manual intervention to start it. cron作业通常可能是一种很好的方法,因为shell方法需要手动干预来启动它。

A couple of suggestions: 一些建议:

You could use a lock file to ensure that the cron job only ever starts one instance of the python script - often problems occur when using cron for larger jobs because it starts a second instance before the first instance has actually finished. 您可以使用锁定文件来确保cron作业只启动python脚本的一个实例 - 通常在将cron用于较大的作业时会出现问题,因为它会在第一个实例实际完成之前启动第二个实例。 You can do this simply by checking whether the lock file exists, then, if it does not, 'touch'ing the file at the beginning of the script and 'rm'ing it as your last action at the end of the script. 您可以通过检查锁定文件是否存在来执行此操作,然后,如果不存在,则在脚本开头“触摸”文件并将其作为脚本末尾的最后一个操作“rm”。 If the lock file exists -- simply exit the script, as there is already one instance running. 如果锁定文件存在 - 只需退出脚本,因为已经有一个实例在运行。 (Of course, if the script dies you will have to delete the lock file before running the script again). (当然,如果脚本死亡,则必须在再次运行脚本之前删除锁定文件)。

Also, if excessive resource use is a problem, you can ensure that the script does not eat too many resources by giving it a low priority (prefix with, for example, nice -n 19). 此外,如果资源使用过多是一个问题,您可以通过赋予它低优先级(前缀例如,nice -n 19)来确保脚本不会占用太多资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 如何在云中安排 python 脚本? - How can I schedule python script in the cloud? 如何使用时间表将脚本设置为每天运行两次? -蟒蛇 - How do I use schedule to set my script run twice a day? - python 如何在 conda env 中安排一个 python 脚本每 n 分钟运行一次? - How can I schedule a python script inside a conda env to run every n minutes? 如何按特定时间表运行云中托管的简单python脚本? - How can I run a simple python script hosted in the cloud on a specific schedule? 从我的Python脚本内部,我想安排另一个Python脚本或命令行脚本的延迟运行 - From inside my Python script, I want to schedule a deferred run of another Python script or a command line script 如何在后台运行我的应用程序 python - How can I run my application in the background python 如何在后台调度 Python Selenium 脚本? - How to schedule a Python Selenium script in the background? 如何安排脚本每天运行 Python - How to schedule a script to run every day for Python 如何安排Python脚本在特定时间运行? - How to schedule a Python script to run at a certain time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM