Timeouting Commands in Shell Scripts
Join the DZone community and get the full member experience.Join For Free
often you want to automatize something using shell scripting. in a perfect world your script robot works for you without getting tired, without hiccups, and you can just sit at the front of your desk and sip coffee.
then we enter the real world: your network is disconnected. dns goes downs. your http hooks and downloads stall. interprocess communication hangs. effectively this means that even if your script is running correctly from the point of operating system it won’t finish its work before you finish your cup of coffee.
below is an example how to create timeouts and notifications in a shell script.
automation must work 100% and you must always know if it doesn’t do that. otherwise if you cannot trust the automated systems you could have this glorious moment of “oh it has been broken for three months now.” no amount of coffee makes your day after that. then you spend your nights pondering whether your scripting is running instead of playing borderlands 2.
some safety guards around your coffee cup include
- sane timeout thresholds for commands to avoid hang situations. e.g. your automated “make smallapp” should probably not run for 50 hours straight.
- get a notification if a fully automatized process does not end up as expected. so that you find out the problem right away, not three months later. do not use email as the communication channel, it is unreliable and has awful signal-to-noise ratio. better solutions include instant messengers ( skype ), sms (check twilio.com)
- get a notification also when a service, which should be running all the time, is restarted.
the shell scripting (sh, zsh, bash) does not offer built-in tools for timeouting commands by default (please correct me if i am wrong, but stackoverflow.com et. al folks did not know any better). in bash cookbook, there exist a timeout wrapper script example ( the orignal so.com answer).
the trick is that when the timeout wrapper terminates the command, you’ll get the exit code of sigterm (143) or sigkill (137) to the parent script. this might not be entirely clear from reading the script.
3. using timeout wrapper and sending failure notifications to skype
below is an example how you can hook timeout to your own script. here we have a simple continous integration script ( ghetto-ci ) which polls version control repositories and on any change executes the test suite. a test failure is reported back to skype by ghetto-ci using sevabot skype bot . the loop script uses a server specific (ubuntu) way to set up a headless x server, so that the tests can run firefox on the server (can one call a headless firefox server “mittens”?) the continous integration loop is deployed simply as leaving it running on the screen ‘ed terminal on a server.
some protection we do: the script has extra checks to protect against hung processes by killing them using pkill before starting each test run. selenium’s webdriver seem to often cause situations where the python tests don’t quit cleanly, leaving around all kind of zombie processes. in this case, the test command timeouts using the timeout wrapper and we get a notification to skype. based on this we can refine the test running logic, timeout delay and such to make the script more robust allowing us to focus more on drinking the coffee and less watching a looping process running in a unix screen for potential failures.
pardon me for possibly ugly shell scripting code. sh is not my primary language. the example code is also on github . please see the orignal blog post for syntax colored example.
Published at DZone with permission of Mikko Ohtamaa, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.