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
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.