{Updated} I want to start command called foo, and kill it if still running after given DURATION in seconds. How do I run a command a time Limit on Linux? How do I run a Linux command, and have it timeout (abort) after N seconds? Can you give timeout command examples in Linux?The post Linux run a command with a time limit (timeout) appeared first on nixCraft.
How to run a command with a time limit on Linux
Our goal is to run a command named “ping www.cyberciti.biz” with a time limit of 30 seconds:
- Open the terminal application
- Run ping command have it abort after 30 seconds: timeout 30s ping www.cyberciti.biz
- One can combine sleep and other shell commands: ping www.cyberciti.biz & sleep 30s; kill $!
Let us see all timeout commands, syntax and examples in Linux operating systems.
Linux time limit command
The syntax is as follows for timeout command:
DURATION is a floating point number with an optional suffix as follows:
timeout DURATION COMMAND
timeout DURATION COMMAND arg1 arg2
timeout 1m ping google.com
timeout 30s tracepath www.cyberciti.biz
timeout [options] DURATION COMMAND arg1 arg2
DURATION is a floating point number with an optional suffix as follows:
- s for seconds (the default).
- m for minutes.
- h for hours.
- d for days.
How to run ping command for 8 seconds and abort it
Try:
date
timeout 8s ping www.cyberciti.biz
timeout 8s ping 192.168.2.254
date
Linux timeout command for ssh
Say you want to start ssh session just for fire mintues for running command/app1 and die after 5 minutes, run:
timeout 5m monitor@server1.cyberciti.biz -- /path/to/app1 --save --force --update
Specify the signal to be sent on timeout
The syntax is:
To get a list of signals, run the following kill command:
Sample outputs:
timeout -s 9 YourCommandHere
timeout --signal=9 YourCommandHere
timeout -s 15 30s tracepath google.com
timeout -s 9 2m tail -F /var/log/secure
## send SIGTERM as terminate signal ##
timeout -s SIGTERM 5m ping google.com
To get a list of signals, run the following kill command:
kill -l
Sample outputs:
How to set grace period
Pass the -k or --kill-after=DURATION options to the timeout command. For example, send a KILL signal if COMMAND is still running this long after the initial signal was sent:
timeout -k=5 2m command1 arg1
timeout -k=5 -s SIGKILL 2m /path/to/my-app arg1 arg2
Other options:
The --preserve-status option allows timeout to exit with the same status as COMMAND, even when the command times out.
The --foreground option when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out:
timeout --preserve-status 10s command1
The --foreground option when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out:
timeout --foreground 1m command2
## Login to remote server. Run htop and die after 30seconds ##
timeout --foreground 30s ssh -t vivek@server1.cyberciti.biz htop
timeout --foreground 20s ssh -t vivek@centos7 top
Bash solution
The syntax is pretty simple with the help of read command and kill command:
The $! contains the process ID (PID) of the most recently executed background pipeline. In this example ping was the most recently executed background job.
MyCoolCommand Arg1 & read -t TIMEOUT_VALUE || kill $!
command arg1 & read -t 30 || kill $!
tail -F /var/log/secure & read -t 60 || kill $!
ping 192.168.2.254 & read -t 10 || kill $!
The $! contains the process ID (PID) of the most recently executed background pipeline. In this example ping was the most recently executed background job.
A note about Perl one liner for Unix/macOS/BSD oses
Try the following combination of Perl and shell function when don’t have or don’t want one of the above programs, you can use a perl one-liner to set an ALRM and then exec the program you want to run under a time limit. In any case, you must understand what your program does with SIGALRM; programs with periodic updates usually use ALRM for that purpose and update rather than dying when they receive that signal.
0 comments:
Post a Comment