Raspberry Pi

How to Run Command with Time Limit in Raspberry Pi

In Linux-based systems such as Raspberry Pi, some commands keep on running for an indefinite time if a specific time limit is not defined for them. Since Raspberry Pi is a low-powered device and keeping different commands running for an indefinite amount of time may reduce the performance of your device.

To keep these commands running for a specific amount of time, “timeout” command is used.

If you don’t know how to use the timeout command on Raspberry Pi, follow this article for detailed guidance.

How to Run a Command with Time Limit in Raspberry Pi?

To run commands with a time limit, the timeout utility is used and the below-mentioned syntax is used for it:

Syntax

$ timeout [time duration] [command] [Argument]

The time duration for the timeout command can be in seconds, minutes, or hours and if the time unit is not defined, it considers the time in seconds by default. The suffixes used for time duration are:

s => Seconds
m => Minutes
h => Hours

The Purpose of Running Commands with Time Limit

There are certain commands in Raspberry Pi that run for an unlimited time period and they won’t stop unless they are terminated manually by pressing keys Ctrl+C. An example of such a command is when you ping the IP address to your Raspberry Pi:

$ ping <IP address>

In the output, you can see that it keeps running and isn’t ending on its own.

So, in order to run such a command for a limited time period, a timeout utility is used and the time period is pre-defined with the command. Now let’s run the same ping command for 3s by using timeout:

Example 1

$ timeout 3s ping <IP address>

In the output, you can see that the command has terminated within 3 seconds:

Example 2
Now let’s run the timeout command with time duration in minutes to ping a web address:

$ timeout 1m ping linuxhint.com

In the output, you will see that the command keeps running for 1 minute:

Example 3
As we have mentioned earlier that if no unit suffix is mentioned for the time duration, then by default it considers seconds as the unit. So, let’s run an example command for this case:

$ timeout 15 ping linuxhint.com

Using Preserve Status With timeout Command

The preserve status is used to return the command exit status; even if the command is terminated forcefully using Ctrl+C keys.

$ timeout --preserve-status 5 ping linuxhint.com

You can use the below-mentioned command to get the exit code:

$ echo $?

Run timeout Command in Foreground

By default, the timeout command runs in the background, However, if a user is interested to run an interactive command that requires users’ input, he/she can run the timeout command in the foreground using the following command:

$ timeout --foreground 5 ping 192.168.18.2

Killing a Stuck Process

Sometimes a command keeps on running on your system even if you want to terminate it on your system. To help you with this, you can use the following command to stop the process at a given time limit.

$ sudo timeout -k 10 1m ping 192.168.18

The above command stops automatically when the defined time limit is achieved as you cannot stop it using “CTRL+C”.

To Get Help With timeout Utility

If at some point you feel the need to check different available options, or even you need help with command syntax you can run the below-mentioned help command to get help:

$ timeout --help

Conclusion

For Raspberry Pi users, there is a very handy command-line utility known as timeout, which allows them to run the commands with the specific time limit. There are different ways to use the timeout utility, which is already discussed in the above guidelines. The users can execute different commands according to his/her requirement. All these commands are useful in setting up a defined time limit for executing the commands on a Raspberry Pi system.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.