Run command with time limit in Linux
Last Updated :
16 Oct, 2024
In Linux, some commands take a really long time to execute, and while we do wait for such commands to finish, sometimes we also want to terminate them after they have been running for a specified amount of time. This is what the 'timeout' command and the `timelimit` program permit us to do. Two methods, then, are described here for automatically terminating long-running commands.
Syntax
The basic syntax of the timeout Command is:
timeout [OPTION] DURATION COMMAND [ARG]...
where,
- DURATION is a number followed by:
- s for seconds; the default if omitted
- m for minutes
- h for hours
- COMMAND is the command to run with a time limit
- [ARG]... any additional arguments for the command
Basic Example
Use the timeout command to bound execution of a ping command:
timeout 5s ping google.com
This will ping google.com for 5 seconds then quit.
timeout command example outputKey Options for the timeout Command
Option | Description |
---|
-s, --signal=SIGNAL | Specify the signal to send when the timeout period ends |
---|
-k, --kill-after=DURATION | Send a KILL signal if COMMAND hadn't terminated this long after the initial signal was sent |
---|
--preserve-status | Exit with the same exit status as COMMAND, even if the command timed out |
---|
Example with --signal option:
timeout --signal=SIGQUIT 8s tail -f /var/log/syslog
This command will tail /var/log/syslog for 8 seconds and then send SIGQUIT.
Example with --kill-after option:
timeout -k 5s 10s sleep 15
It will attempt to send signal 15 to kill the command sleep while it's running in the first 10 seconds, then, if it still hasn't returned yet, kill it after further 5 seconds with signal 9.
Timelimit Program
The `timelimit` program is another alternative to `timeout`, and it offers additional functionality over the extremely flexible tool already available.
Installation
To install it on Debian-based systems, e.g. Ubuntu:
sudo apt install timelimit
For Arch Linux or Arch-based systems:
git clone https://aur.archlinux.org/timelimit.git && cd timelimit && makepkg -sri
Syntax
timelimit [-pq] [-S killsig] [-s warnsig] [-T killtime] [-t warntime] command [arguments ...]
Basic Example
Use 'timelimit' to run a command for a certain amount of time
timelimit -t 10 -T 12 ping google.com
Pings google.com and sends a termination signal after 10 seconds and kills it with a termination signal after 12 seconds.
timelimit command example outputKey Options for the timelimit Program
Option | Description | Default Value |
---|
-t warntime | Time before sending warning signal | 3600 seconds |
---|
-T killtime | Time before sending kill signal | 120 seconds after warntime |
---|
-s warnsig | Signal to send as warning | SIGTERM (15) |
---|
-S killsig | Signal to send to kill | SIGKILL (9) |
---|
Example with custom signals:
timelimit -t 30 -T 35 -s SIGINT -S SIGTERM long_running_script.sh
This will run long_running_script.sh, send a SIGINT after 30 seconds and if it's still running, send a SIGTERM after 35 seconds.
Conclusion
Both `timeout` command and `timelimit` program are powerful control means for long-running commands on the Linux level. Even though the `timeout` is available in any Linux distribution, usually providing minimum functionality, its termination process is less flexible than that one of `timelimit`. Select the right tool for your needs as well as the system configuration.
Similar Reads
time command in Linux with examples 'time' command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates. 'real' time is the time elapsed wall clock time taken by a command to get executed, while 'user' and 'sys' time are the number of
6 min read
cron command in Linux with Examples The cron is a software utility, offered by a Linux-like operating system that automates the scheduled task at a predetermined time. It is a daemon process, which runs as a background process and performs the specified operations at the predefined time when a certain event or condition is triggered w
4 min read
sleep Command in Linux with Examples sleep command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER
5 min read
leave Command in Linux with Examples Many times there comes the situation when we want our terminal to remind us to take a break or to do something important or leave the system for some other reason. So in order to do that we have a command in Linux named leave, thisLinux command sets an alarm on your device for the time mentioned. Th
4 min read
at Command in Linux with Examples In the world of Linux operating systems, there exists a powerful tool known as the "at command." The 'at' command provides users with the ability to schedule tasks to be executed at a later time, offering a convenient way to automate processes without manual intervention. Whether you need to run a s
9 min read
Ulimit, Soft Limits and Hard Limits in Linux ulimit is admin access required Linux shell command which is used to see, set, or limit the resource usage of the current user. It is used to return the number of open file descriptors for each process. It is also used to set restrictions on the resources used by a process. Syntax: To check the ulim
2 min read
chrt command in Linux with examples 'chrt' command in Linux is known for manipulating the real-time attributes of a process. It sets or retrieves the real-time scheduling attributes of an existing PID, or runs the command with the given attributes. 'chrt' can help optimize process management in a Linux system, especially for applicati
4 min read
How to Change the Number of Open File Limit in Linux? If you are an active Linux user, who has to work with many files on Linux at a time then you might have definitely faced a problem regarding âToo many open filesâ on a Linux system. When you have reached the maximum open file limit you will get an error message displaying "Too many open files (24)â
4 min read
Shell Script to Demonstrate Wait Command in Linux Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution
4 min read
What is Real-Time Linux? Real-Time Linux is a specialized operating system kernel designed to provide deterministic and predictable behavior for time-sensitive applications. Unlike traditional Linux distributions, where processes and tasks are scheduled with varying degrees of priority and time-sharing, real-time Linux ensu
3 min read