I like to execute a python script from cron every minute.

I was wondering: this script is gonna send some files via samba to a shared folder. If the process took more than one minute, the next time cron raises the script, would it execute it?

Because the previous script was still being executing.

If this run in this way, I would have to put something to not run the process. For example I had thought in write some file and before executing, read it.

  • Why dont you make an infinite loop within the script, and set some kind of clock or time wait, before it loops again? Then you would only need to run the script at startup – M. Becerra Mar 9 '17 at 17:04

Cron doesn't check whether a previous instance was running or not, for all it knows, it simply checks the time against the conditions. You'll have to put a check in the script to prevent multiple parallel execution.

|improve this answer|||||
  • Thanks very much, what I thought. I will try it. What do you think about the way I said? Is there any better way? Or smarter. – ars1614 Mar 9 '17 at 12:36
  • @ars1614 use fcntl.flock to lock a file. That's probably the safest way to prevent multiple execution. – muru Mar 9 '17 at 12:38
  • @ars1614 almost certainly, but we can't know since you haven't given us any details about what you're trying to do. From what little you say, I would guess the best solution for you would be to use inotify. I suggest you ask a new question and explain your situation in more detail. – terdon Mar 9 '17 at 12:39
  • Ok thanks you two. I will search for those two options and if I do not find any solution I will post again. – ars1614 Mar 9 '17 at 12:41
  • I agree with terdon, polling probably isn't the best thing you can do, so use inotify or, because you are already using Python, use Watchdog: pypi.python.org/pypi/watchdog. – Samuel Mar 19 '17 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.