Single Instance of Bash Script
Hi guys,
I have a bash script which is called automatically I want it so that if when the script is called if a previous instance of it is already running then to delay the running of it until the previous instance has stopped (effectively queue up ./script.sh var1 var2) I have seen some posts about a 'lockfile' but this just seems to stop the second instance running rather than queueing it up to run next (it also needs to be able to queue up a 3rd/4th calling of the script and run them one at a time) any suggestions? p.s. the script is run using cygwin on windows edit: I am very new to bash scripting so simple explanations please |
You can try this code. I think the documentation's enough but you can ask me if there's something you don't understand. I hope you'll also refer to the bash manual first (man bash).
Code:
#!/bin/bash Other than this there should also be other methods like locking files with exec and using fuser to check if a process is still locked with the file; and also repeatedly using rm -f to locked files until the files count turn 1..... Although I still can't see cleaner implementation in those ways and I also find some of it as quirky. You may also just use a single instance file to reference but I didn't choose it since I see a very tiny possibility that a race condition could occur. I get a feeling that other optimistic (ones who'll tend to say "that's ok") programmers may argue with this. You might also want to add a code that automatically checks and/or creates INSTANCEDIR. |
Hi,
@konsolebox not sure if I executed your script as it was intended to. This is what I did: Code:
#!/bin/bash Code:
./script.sh 1 Code:
$ ./caller.sh But as I said, I am not sure if I used it right. <removed: fawlty script> |
sorry my mistake. i forgot to add shopt -s extglob
Code:
#!/bin/bash |
edit: mistakenly pressed quote button for edit.. guess i'll better just post a new complete code. The error messages of kill -s 0 "$PID" is now sent to null.
Code:
#!/bin/bash |
thank you konsolebox
where it says # continue the operations from here I decided to put /path/to/script.sh $1 $2 and call singleinstance.sh $1 $2 in place of the original call (I find it easier to keep the 2 pieces of code separate) but script.sh is saying var 1 is null so it doesn't seem to be passing them any ideas? |
I didn't know that you'd also need to pass the arguments.
Did you already add "$@" to the call to main? Code:
... Code:
/path/to/script.sh "$1" "$2" |
seems to be working good now - thanks konsolebox
|
No prob. You may now mark the thread as solved then.
|
sorry but how to mark it?
|
Thread Tools.
|
the script has been running good for a number of days
but lately not sure why sometimes the script hangs not sure if it is the single instance one or the script i am running through it (need to look into it more - have been away for a few days) and then any other instances get queued up and don't run as the first one never exits could there be an issue if it is called twice very quickly i.e. within 1 second or less? also could it be made so that if the instance has been running for 5 minutes it terminates (therefore allowing any other queued instances to start)? thanks in advance edit: could it be to do with this? http://inamidst.com/eph/cygwin Quote:
Quote:
|
Quite very indirect. I wonder, could the script be sometimes called within an sh.exe environment instead of bash.exe?
Quote:
|
do you mean sometimes run it in bash and sometimes in sh?
i think it is a cygwin error though as it has been happening all the time today... didn't happen at all last week rebaseall helped for a while doesn't anymore |
The script's only meant to run with bash. If sh.exe is your default shell, that might give a difference with operations.
|
All times are GMT -5. The time now is 11:45 PM. |