قالب وردپرس درنا توس
Home / How To / Linux timeout Command explained to beginners (with example)

Linux timeout Command explained to beginners (with example)

Sometimes, when you run a command in Linux, you may want to run it for a certain time. There is a command-line tool – timeout – which is specially developed for this purpose. In this article, we will discuss the basics of this command using some easy-to-read examples.

But before we do, it is worth mentioning that all examples in this guide have been tested on a Ubuntu 18.04 LTS machine.

The Linux timeout command

As the name suggests, the timeout command in Linux allows you to run a command with a time limit. The following is its syntax:


And here's how the tool's handout page explains it:

  timeout - run a command with a time limit 
Start COMMAND, and kill it if it still goes after DURATION

Below are some Q&A-styled examples that should give you a better idea of ​​how this tool works.


Using the timeout command will delete a ping command after 5 seconds, so you can use timeout in this case.

  timeout 5 ping google.com 

Q2. How do you get the command's output status in the output?

By default, if the timeout command succeeds, it returns 124 as the output state. The following is an example:

In the first case, I let the timeout command work successfully, and as you can see, the output state was 124. During the second round, I vigorously killed the command with Ctrl + C, and then case was the initial state 0.

But if you want the timeout to return the command command's status even if the timeout is running, you must use the –preserve status command line options.

For example:

  timeout preserve status 5 ping google.com 

Q3. How to change the signal time output?

No need to guess that timeout works by sending a signal to the input command so that the latter stops running the deadlines. By default, the command uses SIGTERM .

If desired, you can still change the signal sent by timeout. For this, you must use the -s command-line option and set as the signal the signal you want time-out to send.

For example:

  timeout -s SIGKILL [COMMAND] 

To retrieve a list of available signals that you can send, run the following command:

  kill -l 

Q4. How to make timeout automatically sends KILL signal?

Sometimes you may encounter an input command that continues to run even after the timeout sends the start signal. In such cases, timeout may offer an alternative "–kill-after".

Here's how it explains:

  -k, --kill-after = DURATION 

also sends a KILL signal if COMMAND is still running

so long after the original signal was sent

As you can see, you must specify a duration to let timeout know how much time this signal should be sent. [19659032] Conclusion

Depending on the type of work you do, the timeout command may prove to be a pretty handy tool. Here, in this guide, we have discussed the majority of the options that this tool offers. When you are finished training these you can learn more about timeout by going to her husband's side.

Source link