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
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:
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
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:
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:
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.
You can use the below-mentioned command to get the exit code:
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:
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.
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:
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.