Monday, September 11, 2017

Bash script to repeat a task until you tell it to stop

For example, when I am running some task that outputs to the database but takes time to finish, rather than running the SQL myself over and over, I will script it and run the script to repeat. Then I hit ENTER whenever I am ready to find out when it has finished. This becomes more effective when I script the whole task: start the asynchronous job that takes awhile, then repeat-run the monitoring script.

#!/bin/bash
weContinue=true
while "${weContinue}" == true ; do
   "$@"
   echo "[$(date)] Press ENTER to continue; press any other key (then ENTER) to stop."
   read response
   if [ -n "${response}" ] ; then
      weContinue=false
   fi
done

Example running it.

Mon Sep 11 - 11:11 AM > repeat echo "Do something important"
Do something important
[Mon, Sep 11, 2017 11:19:54 AM] Press ENTER to continue; press any other key (then ENTER) to stop.

Do something important
[Mon, Sep 11, 2017 11:19:58 AM] Press ENTER to continue; press any other key (then ENTER) to stop.

Do something important
[Mon, Sep 11, 2017 11:20:01 AM] Press ENTER to continue; press any other key (then ENTER) to stop.
stop thanks