How to Run a Command at a Specific Time in Linux

How to Run a Command at a Specific Time in Linux

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:

$ timeout <time-limit-interval> <command>

For example, the following command will terminate after 10 seconds.

$ timeout 10s tail -f /var/log/pacman.log

You can also omit the suffix s after the seconds. The following commands are the same as above.

$ timeout 10 tail -f /var/log/pacman.log

Other possible suffixes are:

  • m stands for minutes.
  • h stands for hours.
  • d stands for day.

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.

$ timeout -k 20 10 tail -f /var/log/pacman.log

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.

$ man timeout

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:

$ sudo apt-get install timelimit

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:

$ timelimit -t10 tail -f /var/log/pacman.log

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.

$ man timelimit

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:
  • Analyze the basic module management and time management operations of the Linux kernel
  • How to Find the Execution Time of a Command or Process in Linux
  • Detailed explanation of 2 methods to synchronize network time in Linux/CentOS system
  • 15 Linux Command Aliases That Will Save You Time
  • Linux date time setting synchronization command sharing
  • Detailed explanation of Linux NTP server time synchronization settings
  • Linux batch delete file command by time (delete files N days ago)
  • Tutorial on configuring and using i3 window manager in Linux
  • Use iptables and firewalld tools to manage Linux firewall connection rules
  • 8 commands to effectively manage processes in Linux
  • Linux kernel device driver kernel time management notes

<<:  JavaScript implements a box that follows the mouse movement

>>:  MySQL log system detailed information sharing

Recommend

Detailed explanation of Linux netfilter/iptables knowledge points

Netfilter Netfilter is a packet processing module...

Source code reveals why Vue2 this can directly obtain data and methods

Table of contents 1. Example: this can directly g...

Three ways to draw a heart shape with CSS

Below, we introduce three ways to draw heart shap...

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

Analysis of problems caused by MySQL case sensitivity

MYSQL is case sensitive Seeing the words is belie...

Detailed example of mysql similar to oracle rownum writing

Rownum is a unique way of writing in Oracle. In O...

Detailed explanation of Docker data backup and recovery process

The data backup operation is very easy. Execute t...

Example code for circular hover effect using CSS Transitions

This article introduces Online preview and downlo...

JavaScript to achieve text expansion and collapse effect

The implementation of expanding and collapsing li...

Multiple solutions for cross-domain reasons in web development

Table of contents Cross-domain reasons JSONP Ngin...

Docker+gitlab+jenkins builds automated deployment from scratch

Table of contents Preface: 1. Install Docker 2. I...

Do you know how to optimize loading web fonts?

Just as the title! The commonly used font-family l...

Detailed explanation of how to quickly build a blog website using Docker

Table of contents 1. Preparation 2. Deployment Pr...