Quote Originally Posted by oj43085 View Post
If i want to include this file lock into my crontab scheduled tasks, then should crontab call another .sh called, for example, execute_task1.sh and then the contents of execute_task1.sh can be:
Code:
 exec 9>/path/to/lock/file
  if ! flock -n 9  ; then
     echo "another instance is running";
     exit 1
  fi
else
/rsync_jobs/job1.sh
  # this now runs under the lock until 9 is closed (it will be closed automatically when the script ends)
What you post would work, assuming nothing else is going to call job1.sh. But unless execute_task1.sh is going to more than just job1.sh then put the locking stuff in job1.sh and call job1.sh from crontab.

It might help if you made your self a simple script to see how the locking works. E.g.
Code:
#!/bin/bash

exec 9>/tmp/$(basename $0)_lock
if ! flock -n 9  ; then
     echo "another instance is running";
     exit 1
fi

sleep 600
Run that, then try running another instance of it.