The other day I was transferring a large file using rsync to another system on my local area network. Since it is very big file, It took around 20 minutes to complete. I don’t want to wait that much longer, and also I don’t want to manually terminate the process by pressing CTRL+C either. I was just wondering if there could be any easy ways to run a command for a specific time and kill it automatically once the time is out in Unix-like operating systems – hence this post. Read on.
Run A Command For A Specific Time In Linux
We can do this in two methods.
Method 1 – Using “timeout” command
The most common method is using timeout command for this purpose. For those who don’t know, the timeout command will effectively limit the absolute execution time of a process. The timeout command is part of the GNU coreutils package, so it comes pre-installed in all GNU/Linux systems.
Let us say, you want to run a command for only a specific time, and then kill it automatically once the time is passed. To do so, we use:
For example, the following command will terminate after 10 seconds.
You also don’t have to specify the suffix “s” for seconds. The following command is same as above.
The other available suffixes are:
- ‘m’ for minutes,
- ‘h’ for hours
- ‘d’ for days.
If you run this tail -f /var/log/pacman.log command, it will keep running until you manually end it by pressing CTRL+C. However, if you run it along with timeout command, it will be killed automatically after the given time interval.
Just in case if the command is still running even after the time out, you can send a kill signal like below.
In this case, if you the tail command still running after 10 seconds, the timeout command will send it a kill signal after 20 seconds and end it.
The Tmeout command can be especially useful when troubleshooting hardware issues. For instance, run the following command to display all messages from the kernel ring buffer, but for only 10 seconds.
For more details, check the man pages.
Sometimes, a particular program might take long time to complete and end up freezing your system. In such cases, you can use this trick to end the process automatically after a particular time.
Also, consider using Cpulimit , a simple application to limit the CPU usage of a process. For more details, check the following link.
Method 2 – Using “Timelimit” program
The Timelimit utility executes a given command with the supplied arguments and terminates the spawned process after a given time with a given signal. First, it will pass the warning signal and then after timeout, it will send the kill signal.
Unlike the timeout utility, the Timelimit has more options. You can pass number of arguments such as killsig, warnsig, killtime, warntime etc.
It is available in the default repositories of Debian-based systems. So, you can install it using command:
For Arch-based systems, it is available in the AUR. So, you can install it using any AUR helper programs such as Pacaur, Packer, Yay, Yaourt etc.
For other distributions, download the source from here and manually install it.
After installing Timelimit program, run the following command for a specific time, for example 10 seconds:
If you run timelimit without any arguments, it will use the default values: warntime=3600 seconds, warnsig=15, killtime=120, killsig=9.
For more details, refer the man pages and the project’s website given at the end of this guide.
And, that’s all for today. I hope this was useful. More good stuffs to come. Stay tuned!
Cheers!
'LINUX' 카테고리의 다른 글
RHEL에서 netstat 프로세스의 PID / Program Name을 표시하지 않는 이유는 무엇인가요? (0) | 2020.12.22 |
---|---|
packet_write_wait: Connection to X.X.X.X port 22: Broken pipe (0) | 2019.09.09 |
How to get high precision nano second delay in linux III (0) | 2019.03.04 |
How to get high precision nano second delay in linux II (0) | 2019.03.04 |
How to get high precision nano second delay in linux I (0) | 2019.03.04 |
ktime_get()
; the real delay is smaller. – CL. Mar 30 '16 at 10:49