This is great for single use scripts, but what about if we wanted to have scripts execute multiple scripts through a menu, or perform tasks in the background automatically forever without being executed each time by scheduling processes (like cron)? This recipe introduces a few ways for a script to run forever until it is killed or exits.
Prerequisites
Besides having a terminal open, we need to remember a few concepts:
- Recursive functions combined with a prompt (for example, the
read
command) can result in a script that loops based on user input - Looping constructs such as
for
,while
, anduntil
can be executed in such a way that a condition is never met and cannot exit
Therefore, a loop or something that causes a loop will force a program to run for an indefinite period of time until an exit event occurs.
Note:
In many programming languages, a program will be executed through the concept of a main function. Within this main
function, often programmers create what’s called a run loop, which allows the program to run forever (even if it is doing nothing).
Using the recursive method, the operation may look like this:
- The script or program enters a recursive function
- The recursive function can continue calling itself indefinitely, or, wait for a blocking input (for example, the
read
command) - Based on the input provided by the
read
command, you could call the same function again - Go back to step 1 until exit
Alternatively, the looping mechanism is similar except functions are not necessarily executed. A loop with an effectively unreachable condition will continuously run unless something interrupts the execution (for example, sleep
).
Note:
A loop running continuously without pause will use CPU resources that could be used elsewhere or waste CPU cycles. If you are running on a battery or a resource constrained platform, extra CPU activity is best to be avoided where possible.
Note:
Using the sleep
command is an excellent way to limit CPU usage when using loops in simple scripts. However, time adds up if you are running a long script!
Write Script:
Let’s start our activity as follows:
Open a terminal and create the recursive_read_input.sh
script:
recursive_read_input.sh
#!/bin/bash
function recursive_func() {
echo -n "Press anything to continue loop "
read input
recursive_func
}
recursive_func
exit 0
Execute the $ bash recursive_read_input.sh
script—press Enter at the prompt and wait for another prompt.
Exit the program with Ctrl + C.
Open a terminal and create the loop_for_input.sh
script:
loop_for_input.sh
#!/bin/bash
for(( ; ; ))doecho"Shall run for ever"sleep1done
exit 0
Execute the $ bash loop_for_input.sh
script—press Enter at the prompt and wait for another prompt.
Exit the program with Ctrl + C.
Open a terminal and create the loop_while_input.sh
script:
loop_while_input.sh
#!/bin/bash
EXIT_PLEASE=0
while : # Notice no conditions?
do
echo "Pres CTRL+C to stop..."
sleep 1
if [ $EXIT_PLEASE != 0 ]; then
break
fi
done
exit 0
Execute the $ bash loop_while_input.sh
script—press Enter at the prompt and wait for another prompt.
Exit the program with Ctrl + C.
Open a terminal and create the loop_until_input.sh
script:
loop_until_input.sh
#!/bin/bash
EXIT_PLEASE=0
until [ $EXIT_PLEASE != 0 ] # EXIT_PLEASE is set to 0, until will never be satisfied
do
echo "Pres CTRL+C to stop..."
sleep 1
done
exit 0
Execute the $ bash loop_until_input.sh
script—press Enter at the prompt and wait for another prompt.
Exit the program with Ctrl + C.
How these scripts work:
Let’s understand our script in detail:
Creating the recursive_read_input.sh
script is a simple process. We can see that the read
command expects input (and will store it in the $input
variable), then the script calls recursive_func()
again for each time a read exits.
Executing the script with $ bash recursive_read_input.sh
runs the script indefinitely. No matter the input, Ctrl + C or killing the script will exit it.
Creating the loop_for_input.sh
script is relatively trivial as well. We can notice two things: the for loop has no parameters, except for (( ; ; ))
and the sleep
command. This will make it run forever, but upon each execution of the loop, it will echo Shall run for ever
to the console and sleep one second before continuing to the next loop.
Executing the $ bash loop_for_input.sh
script will cause the script to loop forever.
Ctrl + C will cause the script to exit.
Use the loop_while_input.sh
script by using a while
loop with the : noop
command. However, there is a small difference of an if statement (which will never evaluate to true), but it can still be used in another script to set a condition which causes the script to break the while
loop and exit.
Executing the $ bash loop_while_input.sh
script will cause the script to loop forever.
Ctrl + C will cause the script to exit.
The loop_until_input.sh
script is similar to the while
loop forever example, but it is different because you can also embed a condition, which will never evaluate to true.
This causes the script to loop forever unless the $EXIT_PLEASE
variable is set to 1
.
Executing the $ bash loop_until_input.sh
script will cause the script to loop forever.
Ctrl + C will cause the script to exit.
0 Comments