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 the basic commands of Firewalld firewall in Centos7

1. Basics of Linux Firewall The Linux firewall sy...

How to migrate local mysql to server database

We can use the scp command of Linux (scp cannot b...

Implementation of CSS text shadow gradually blurring effect

text-shadow Add a shadow to the text. You can add...

Steps to repair grub.cfg file corruption in Linux system

Table of contents 1. Introduction to grub.cfg fil...

Zen coding for editplus example code description

For example, he enters: XML/HTML Code div#page>...

Detailed discussion of the character order of mysql order by in (recommended)

//MySQL statement SELECT * FROM `MyTable` WHERE `...

JS implements a simple counter

Use HTML CSS and JavaScript to implement a simple...

Detailed explanation of HTML's <input> tag and how to disable it

Definition and Usage The <input> tag is use...

Detailed steps for completely uninstalling MySQL 5.7

This article mainly summarizes various problems o...

Detailed explanation of four solutions to floating problems in CSS layout

1. Cause: The effect after the subbox is set to f...

Detailed explanation of Jquery datagrid query

Table of contents Add code to the Tree item; 1. S...

Practical record of vue using echarts word cloud chart

echarts word cloud is an extension of echarts htt...

MySQL 8.x msi version installation tutorial with pictures and text

1. Download MySQL Official website download addre...

Web development tutorial cross-domain solution detailed explanation

Preface This article mainly introduces the cross-...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...