linux - Re-run bash script if another instance was invoked -
i have bash script may invoked multiple times simultaneously. protect state information (saved in /tmp file) script accesses, using file locking this:
do_something() { ... } // check if there other instances of script; if exit exec 8>$lock if ! flock -n -x 8; exit 1 fi // script something... do_something now other instance invoked when script running exits. want script run 1 time if there n simultaneous invocations, not n-times, this:
do_something() { ... } // check if there other instances of script; if exit exec 8>$lock if ! flock -n -x 8; exit 1 fi // script something... do_something // check if instance invoked, if re-run do_something again if [ condition ]; do_something fi how can go doing this? touching file inside flock before quitting , having file condition second if doesn't seem work.
have 1 flag (lockfile) signal things needs doing, , set it. have separate flag unset execution part.
request_file=/tmp/please_do_something lock_file=/tmp/doing_something # request running touch $request_file # lock , run if ln -s /proc/$$ $lock_file 2>/dev/null ; while [ -e $request_file ]; do_something rm $request_file done rm $lock_file fi if want ensure "do_something" run once each time whole script run, need create kind of queue. overall structure similar.
Comments
Post a Comment