Scripts in slumberland
AnthonyEnglish 270000RKFN Visits (4197)
Whenever I see a script with a five or ten minute wait in it, I ask myself what it's waiting for. I recently saw a script which started a database, waited 10 minutes (using sleep 600), then started an app server (sleep 600), then started another application (sleep 600, then exit!). That script gets a lot of sleep.
The reason behind the delays was to wait for the previous process to complete. But that could be confirmed in a few ways. Ordinarily, the previous command will have an exit code which you can test for, or you could use the Logical AND (&&) operator.
cmd1 && cmd2
This means only run cmd2 if cmd1 completes successfully (exit code 0).
If the first process is run in background, then you may have reason to delay before proceeding to the next process. But a ten minute wait is no guarantee that the first process completed successfully, so it would be smarter to use a shorter delay, or even set up a loop which checks periodically that the first process has completed.
In the example script I gave above, the procedure delayed 600 seconds (that's 10 minutes if you're not a mathematician) , then 600 seconds then 600 seconds again, or 30 minutes in total. When the script was first written it was meant for a single database. But then the script was used for two other databases, and soon enough a restart would take 90 minutes out of an outage window.
If you really must do a sleep in your script, set the time as a variable in the start of the script or passed as an argument from the command line:
bigsleep=600 # Wait 10 mins for something (or nothing) to happen
Then you can do the sleep if you really need to: