Timeout Command in Linux - MS TV Life.COM

Timeout Command in Linux

Timeout Command in Linux

timeout is a command-line utility that runs a specified command and terminates it whether it is nonetheless working after a given time period. In different phrases, timeout lets you run a command with a time restrict. The timeout command is part of the GNU core utilities package deal which is put in on virtually any Linux distribution.

How To Set or Change Timezone on CentOS 8

It’s useful once you need to run a command that doesn’t have a built-in timeout choice.

On this article, we’ll clarify how one can use the Linux timeout command.

How one can Use the timeout Command #

The syntax for the timeout command is as follows:

timeout [OPTIONS] DURATION COMMAND [ARG]…

The DURATION is usually a constructive integer or a floating-point quantity, adopted by an non-obligatory unit suffix:

  • s – seconds (default)
  • m – minutes
  • h – hours
  • d – days

When no unit is used, it defaults to seconds. If the length is ready to zero, the related timeout is disabled.

The command choices have to be supplied earlier than the arguments.

Listed here are a couple of fundamental examples demonstrating how one can use the timeout command:

  • Terminate a command after 5 seconds: timeout 5 ping 8.8.8.8
  • Terminate a command after 5 minutes: timeout 5m ping 8.8.8.8
  • Terminate a command after one minute and 6 seconds: timeout 1.1m ping 8.8.8.8

If you wish to run a command that requires elevated privileges reminiscent of tcpdump, prepend sudo earlier than timeout:

sudo timeout 300 tcpdump -n -w data.pcap

Sending Particular Sign #

If no sign is given, timeout sends the SIGTERM sign to the managed command when the time restrict is reached. You may specify which sign to ship utilizing the -s (--signal) choice.

How to Auto-Improve Server Response Time and speed

For instance, to ship SIGKILL to the ping command after one minute you’d use:

sudo timeout -s SIGKILL ping 8.8.8.8

The sign will be specified by its title like SIGKILL or its quantity like 9. The next command is an identical to the earlier one:

sudo timeout -s 9 ping 8.8.8.8

To get an inventory of all out there alerts, use the kill -l command:

kill -l

Killing Caught Processes #

SIGTERM, the default sign that’s despatched when the time restrict is exceeded will be caught or ignored by some processes. In that conditions, the method continues to run after the termination sign is ship.

To verify the monitored command is killed, use the -k (--kill-after) choice following by a time interval. When this selection is used after the given time restrict is reached, the timeout command sends SIGKILL sign to the managed program that can’t be caught or ignored.

Within the following instance, timeout runs the command for one minute, and if it’s not terminated, it’ll kill it after ten seconds:

sudo timeout -k 10 1m ping 8.8.8.8

timeout -k “./check.sh”

killed after the given time restrict is reached

Preserving the Exit Standing #

timeout returns 124 when the time restrict is reached. In any other case, it returns the exit standing of the managed command.

To return the exit standing of the command even when the time restrict is reached, use the --preserve-status choice:

timeout --preserve-status 5 ping 8.8.8.8

Working in Foreground #

By default, timeout runs the managed command within the background. If you wish to run the command within the foreground, use the --foreground choice:

timeout --foreground 5m ./script.sh

This selection is helpful once you need to run an interactive command that requires person enter.

Conclusion #

The timeout command is used to run a given command with a time restrict.

timeout is an easy command that doesn’t have quite a lot of choices. Usually you’ll invoke timeout solely with two arguments, the length, and the managed command.

How to Install GCC (Development Tools) on CentOS 8

When you’ve got any questions or suggestions, be happy to go away a remark.

Copy link
Powered by Social Snap