Continuously re-execute a command when it finishes in Bash (7 Solutions!!)
Channel: Roel Van de Paar
Date: 19/08/2020

Continuously re-execute a command when it finishes in Bash


The Question: I'm trying to figure out a simple way to keep re-executing a command whenever
it finishes, for any reason (error or otherwise).
What would be the best way to approach this?
EDIT: I wasn't clear enough before. I need the next execution to wait until the
previous one finishes.
EDIT 2: Almost all answers work great. I mistakenly thought most answers would
either fork the process or re-execute it just once, neither of which is wanted.

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 40 people ==
This creates an infinite loop, executing command over and over again.
while :
do
command
done

== This solution helped 7 people ==
You can plan ahead during command execution (provided it isn't interactive) and
enter !! which will execute the previous command again. This works e.g. during
ping -c 5.
----
You can also define a function in bash:
function repeat { "$@"; "$@"; }
To persist it, store it in ~/.bashrc.
Then, you can run your command like this:
repeat ping -c5 heise.de
If it's a specific command you want to repeatedly execute (and not e.g. any
command), you can replace "$@" in that snippet with your actual command, and
name the function e.g. repeat_ping instead.
----
To make it an infinite loop, you can do what @Dennis suggests. I recommend you
add a waiting period if you intend to use this in an interactive shell, like
this:
function repeat { while 1 ; do "$@" ; sleep 1 ; done; }
Otherwise it's rather inconvenient to abort this infinite loop using Ctrl-Z.

== This solution helped 18 people ==
A simple solution would be:
yourcommand; !#
; separates commands, allowing for multiple commands in one line (http://
www.gnu.org/software/bash/manual/bashref.html#Lists)
!# tells bash to "repeat everything I have written so far in this line" (http:/
/www.gnu.org/software/bash/manual/bashref.html#Event-Designators)

== This solution helped 110 people ==
The watch command will repeat a command forever with an interval specified:
watch -n0 ＜command＞
Setting -n to zero effectively puts the interval at nothing (I think it is
really .1 seconds).
watch also has the added benefits of aligning the output so visual changes can
be seen easily, and has a switch to highlight changes from the last run.
http://linux.die.net/man/1/watch:
 watch runs command repeatedly, displaying its output (the first
 screenfull). This allows you to watch the program output change over
 time. By default, the program is run every 2 seconds; use -n or --
 interval to specify a different interval.
 watch will run until interrupted.

This is another method I use to repeat a command in bash. This might sound
silly to some as it does not involve writing a script and might be known to
many. But I think it is worth mentioning as it is quick and beginner-friendly,
in the sense that there is not much syntax to remember. I've only tested this
in gnome-terminal of Ubuntu and other terminals might not support this.
 1. Enter the command in terminal once.
 2. Select the command including newline character with mouse.
 3. Copy the selection using Ctrl-Insert or using context menu (usually
 accessed with right click).
 4. Use Shift-Insert to paste as many times as you want to run the command.
You don't have to wait to paste until each command finishes. The pasted
commands get into a queue and they execute one after another.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to the Stack Exchange Network (http://superuser.com/questions/361902). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.