1

I have this Django cron job script (I am using kronos for that, which is great).

Since I trigger this job every minute, I want to make sure that there isn't another instance of the script already running. If there is a previous job running, then I want to skip the current execution.

I know I can do that with a lock file, but that's not very reliable and can cause problems when you reboot in the middle of an execution (you have to clear the lock file), etc.

What's the best way to do this using Python (Django in this case)?

EDIT: I am targeting Linux, sorry for leaving this out.

kolrie
  • 12,562
  • 14
  • 64
  • 98
  • 1
    On what platform? A lock-file is perfectly reliable on most Unix/Linux platforms these days: use os.open with O_CREAT|O_EXCL which guarantees only a single process has access to the file and a path which corresponds to a memory or swap based filesystem (like tmpfs: /tmp on Solaris, /run or /var/run on Linux) which solves your reboot concerns. The only potential issue is if your process crashes, doesn't clean up, etc... – isedev Mar 22 '13 at 01:45
  • Another option would be to use a kernel-based semaphore. – isedev Mar 22 '13 at 01:47
  • Sorry guys, Linux :-) – kolrie Mar 22 '13 at 02:50
  • I'd also love to know this :) – DanH Apr 19 '13 at 06:15

1 Answers1

0

There's a Django app here: https://github.com/jsocol/django-cronjobs

Also available on pip as cronjobs.

Just in case you go straight to pip; You register jobs using the decorator like so:

# myapp/cron.py
import cronjobs

@cronjobs.register
def periodic_task():
    pass

Then run the command via:

$ ./manage.py cron periodic_task

It has job locking by default but you can disable it when you apply the decorator:

@register(lock=False)
DanH
  • 5,498
  • 4
  • 49
  • 72