Skip to content

Instantly share code, notes, and snippets.

@peabnuts123
Created August 30, 2017 10:31
Show Gist options
  • Save peabnuts123/05cf1ea54265d118dc8967a526b62487 to your computer and use it in GitHub Desktop.
Save peabnuts123/05cf1ea54265d118dc8967a526b62487 to your computer and use it in GitHub Desktop.
Run non-terminating commands with the ability to force restart them
#!/bin/bash
# Validate argument exists
if [ -z "$1" ]
then
echo "No command specified."
echo "Usage: loop-command (command)"
exit 1
fi
# Get command from first argument
child_command="$1"
# Preamble
echo " --- Command Looper by Peabnuts123 --- "
echo "*** Press Ctrl+C once to restart the command"
echo "*** Press Ctrl+C twice in succession to exit"
sleep 3
# Release SIGINT trap, sleep, giving the user
# the opportunity to SIGINT again
function restart_or_kill() {
trap - INT
echo "*** Send another SIGINT to exit"
sleep 1
}
# Loop!
while true
do
echo "*** Executing \`${child_command}'..."
trap restart_or_kill INT
eval $child_command
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment