How it works...

You might be asking yourself, can I put functions to the background? Absolutelyand you could even use a command called export with the -f flag (although it may not be supported in all environments). If you were to use the timeout command instead, you would have to either run ONLY the command you wish to monitor or put the function inside of a second script to be called by timeout. Clearly, this is less than optimal in some situations. In this recipe, we use signals or rather, the alarm signal, to act as a timer. When we set the alarm with a specific variable, it will raise SIGALARM once the timer expires! If the process is still alive, we merely kill it and exit the script if we haven't already exited:

  1. In step 1, we create ...

Get Bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.