The other day I was using rsync to transfer a large file to another system on my LAN. Since it is a very large file, it will take about 20 minutes to complete. I don't want to wait any longer, and I don't want to press CTRL+C to terminate the process. I just wanted to know if there was an easy way in Linux OS to run a command for a specific time and automatically kill it once it times out – hence this post. Please continue reading. Run a command at a specific time in Linux We can do this in two ways. Method 1 – Using the timeout command The most common method is to use the timeout command. For those who don't know, the timeout command effectively limits the absolute execution time of a process. The timeout command is part of the GNU coreutils package, so it comes preinstalled on all GNU/Linux systems. Let's say you only want to run a command for 5 seconds and then kill it. To do this we use: For example, the following command will terminate after 10 seconds. You can also omit the suffix s after the seconds. The following commands are the same as above. Other possible suffixes are:
If you run the command tail -f /var/log/pacman.log it will continue to run until you manually end it by pressing CTRL+C. However, if you run it with the timeout command, it will automatically terminate after the given time interval. If the command is still running after the timeout, you can send a kill signal as shown below. In this case, if the tail command is still running after 10 seconds, the timeout command will send a kill signal after 20 seconds and end. For more details, check the man page. Sometimes, a particular program may take a long time to complete and end up freezing your system. In this case, you can use this trick to automatically end the process after a certain amount of time. Method 2 - Using the timelimit program timelimit Executes the given command with the supplied arguments and terminates the process with the given signal after the given time. First, it sends a warning signal, and then after a timeout, it sends a kill signal. Different from timeout, timelimit has more options. You can pass number of arguments like 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 AUR. So, you can install it using any AUR helper like Pacaur, Packer, Yay, Yaourt, etc. For other distributions, please download the source here and install manually. After installing timelimit, run the following command to execute for a specific period of time, for example 10 seconds: If you run timelimit without any arguments, it uses the default values: warntime=3600 seconds, warnsig=15 seconds, killtime=120 seconds, killsig=9. For more details, see the man pages given at the end of this guide and the project website. Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links You may also be interested in:
|
<<: JavaScript implements a box that follows the mouse movement
>>: MySQL log system detailed information sharing
What is CSS# CSS (abbreviation of Cascading Style...
As shown below: As shown above, just replace it. ...
1. What is the hyperlink icon specification ?<...
Table of contents 1. prototype (explicit prototyp...
The default storage directory of mysql is /var/li...
Part 1 HTML <html> -- start tag <head>...
Meta declaration annotation steps: 1. Sort out all...
How to add css in html? There are three ways to s...
Table of contents Preface Solution Specific imple...
This article example shares the specific code of ...
This article shares the installation tutorial of ...
Preview knowledge points. Animation Frames Backgr...
Preface Readers who are familiar with MySQL may f...
This article mainly introduces the layout method ...
Focus images are a way of presenting content that ...