1

I have a directory to monitor. When it is updated, I run some command via incron. When multiple files are copied in this directory, incron execute multiple commands at the same time. Is there any way that when one job of incron is running, the second job should not run?. I have followed this tutorial for guidance.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 1
    I think you're better off making sure your script handles the concurrency. E.g. Touch a lock file in /tmp on start if it doesn't exist, exit immediately if it exists, something like that. Icron will trigger every time your rule fires which is probably whenever a file has been written to that directory. It cannot know when you're done writing to that directory. You could also create a timer that is triggered when a file is added to the dir and will wait e.g. 5 minutes before executing your script (which again locks) so as to wait for all files to be copied. – kba Feb 17 '16 at 11:37

2 Answers2

1

No, incron doesn't have a built-in lock feature. If you want to prevent jobs from running at the same time, do it from within the job.

If you want to delay a job until the previous one(s) have finished, make them take a lock. You can use the flock command. There are examples in the man page.

If you want to skip a job if the previous one hasn't finished, you can still use flock, but with a timeout of 0. If you can't obtain the lock, exit. Note that this is prone to a race condition: it could happen that a new file is copied just after the time job #1 had finished enumerating files but before it had time to release the lock, and job #2 would see that the lock is still held and exit without processing the file either. There's no easy way to solve that race.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
0

One simplest solution example: suppose 1 means file is locked (previous script is already running) and 0 means unlocked (No process running).

Then here is the code

    state=`cat /tmp/my_lck`
    if [ "$state" -eq 1 ];then
        echo "Already executing!!!"
        exit
    else
        #Lock the file
        echo 1 > /tmp/nws_lck

       #Put you code here  

 #Unlock    
        echo 0 > /tmp/nws_lck

    fi