Home » BASH » Write a bash script to running a program continuously (forever) using looping constructs or recursion

Write a bash script to running a program continuously (forever) using looping constructs or recursion

Update on:
May 14, 2021

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.


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 forwhile, and until 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.


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:

  1. The script or program enters a recursive function
  2. The recursive function can continue calling itself indefinitely, or, wait for a blocking input (for example, the read command)
  3. Based on the input provided by the read command, you could call the same function again
  4. 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).


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.


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:

[et_pb_dmb_code_snippet title=”recursive_read_input.sh” code=”IyEvYmluL2Jhc2ggCgpmdW5jdGlvbiByZWN1cnNpdmVfZnVuYygpIHsgCgllY2hvIC1uICJQcmVzcyBhbnl0aGluZyB0byBjb250aW51ZSBsb29wICIgCglyZWFkIGlucHV0IAoJcmVjdXJzaXZlX2Z1bmMgCn0gCgpyZWN1cnNpdmVfZnVuYyAKZXhpdCAw” _builder_version=”4.9.4″ _module_preset=”369a0a83-f997-4079-bcdc-749886c9e1c7″ hover_enabled=”0″ sticky_enabled=”0″]IyEvYmluL2Jhc2ggCgpmdW5jdGlvbiByZWN1cnNpdmVfZnVuYygpIHsgCgllY2hvIC1uICJQcmVzcyBhbnl0aGluZyB0byBjb250aW51ZSBsb29wICIgCglyZWFkIGlucHV0IAoJcmVjdXJzaXZlX2Z1bmMgCn0gCgpyZWN1cnNpdmVfZnVuYyAKZXhpdCAw[/et_pb_dmb_code_snippet]

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:

[et_pb_dmb_code_snippet title=”loop_for_input.sh” code=”IyEvYmluL2Jhc2ggCmZvcigoIDsgOyApKWRvZWNobyJTaGFsbCBydW4gZm9yIGV2ZXIic2xlZXAxZG9uZSAKZXhpdCAw” _builder_version=”4.9.4″ _module_preset=”369a0a83-f997-4079-bcdc-749886c9e1c7″ hover_enabled=”0″ sticky_enabled=”0″]IyEvYmluL2Jhc2ggCmZvcigoIDsgOyApKWRvZWNobyJTaGFsbCBydW4gZm9yIGV2ZXIic2xlZXAxZG9uZSAKZXhpdCAw[/et_pb_dmb_code_snippet]

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:

[et_pb_dmb_code_snippet title=”loop_while_input.sh” code=”IyEvYmluL2Jhc2ggCkVYSVRfUExFQVNFPTAgCndoaWxlIDogIyBOb3RpY2Ugbm8gY29uZGl0aW9ucz8gCmRvIAoJZWNobyAiUHJlcyBDVFJMK0MgdG8gc3RvcC4uLiIgCglzbGVlcCAxIAoJaWYgWyAkRVhJVF9QTEVBU0UgIT0gMCBdOyB0aGVuIAoJCWJyZWFrIAoJZmkgCmRvbmUgCmV4aXQgMA==” _builder_version=”4.9.4″ _module_preset=”369a0a83-f997-4079-bcdc-749886c9e1c7″ hover_enabled=”0″ sticky_enabled=”0″]IyEvYmluL2Jhc2ggCkVYSVRfUExFQVNFPTAgCndoaWxlIDogIyBOb3RpY2Ugbm8gY29uZGl0aW9ucz8gCmRvIAoJZWNobyAiUHJlcyBDVFJMK0MgdG8gc3RvcC4uLiIgCglzbGVlcCAxIAoJaWYgWyAkRVhJVF9QTEVBU0UgIT0gMCBdOyB0aGVuIAoJCWJyZWFrIAoJZmkgCmRvbmUgCmV4aXQgMA==[/et_pb_dmb_code_snippet]

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:

[et_pb_dmb_code_snippet title=”loop_until_input.sh” code=”IyEvYmluL2Jhc2ggCkVYSVRfUExFQVNFPTAgCnVudGlsIFsgJEVYSVRfUExFQVNFICE9IDAgXSAjIEVYSVRfUExFQVNFIGlzIHNldCB0byAwLCB1bnRpbCB3aWxsIG5ldmVyIGJlIHNhdGlzZmllZCAKZG8gCgllY2hvICJQcmVzIENUUkwrQyB0byBzdG9wLi4uIiAKCXNsZWVwIDEgCmRvbmUgCmV4aXQgMA==” _builder_version=”4.9.4″ _module_preset=”369a0a83-f997-4079-bcdc-749886c9e1c7″ hover_enabled=”0″ sticky_enabled=”0″]IyEvYmluL2Jhc2ggCkVYSVRfUExFQVNFPTAgCnVudGlsIFsgJEVYSVRfUExFQVNFICE9IDAgXSAjIEVYSVRfUExFQVNFIGlzIHNldCB0byAwLCB1bnRpbCB3aWxsIG5ldmVyIGJlIHNhdGlzZmllZCAKZG8gCgllY2hvICJQcmVzIENUUkwrQyB0byBzdG9wLi4uIiAKCXNsZWVwIDEgCmRvbmUgCmV4aXQgMA==[/et_pb_dmb_code_snippet]

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.

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!



Submit a Comment

Your email address will not be published.

four − 2 =