Running python script with cron only if not running

后端 未结 4 1634
自闭症患者
自闭症患者 2020-12-25 14:45

I need to run a python script (job.py) every minute. This script must not be started if it is already running. Its execution time can be between 10 seconds and several hours

4条回答
  •  無奈伤痛
    2020-12-25 15:12

    The only suggestion I would make is to make your exception handling a little more specific. You don't want to accidentally delete the fcntl import one day and hide the NameError that results. Always try to catch the most specific exception you want to handle. In this case, I suggest something like:

    import errno
    
    try:
        fcntl.lock(...)
    except IOError, e:
        if e.errno == errno.EAGAIN:
            sys.stderr.write(...)
            sys.exit(-1)
        raise
    

    This way, any other cause of the lock being unobtainable shows up (probably in your email since you're using cron) and you can decide if it's something for an administrator to look at, another case for the program to handle, or something else.

提交回复
热议问题