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

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

Hyperlink icon specifications: improve article readability

1. What is the hyperlink icon specification ?<...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

Implementation of mysql8.0.11 data directory migration

The default storage directory of mysql is /var/li...

HTML Basics_General Tags, Common Tags and Tables

Part 1 HTML <html> -- start tag <head>...

Meta declaration annotation steps

Meta declaration annotation steps: 1. Sort out all...

Detailed explanation of the difference between CSS link and @import

How to add css in html? There are three ways to s...

A brief discussion on using virtual lists to optimize tables in el-table

Table of contents Preface Solution Specific imple...

Vue project implements graphic verification code

This article example shares the specific code of ...

MySQL 8.0.12 Simple Installation Tutorial

This article shares the installation tutorial of ...

CSS3 uses var() and calc() functions to achieve animation effects

Preview knowledge points. Animation Frames Backgr...

A brief analysis of the best way to deal with forgotten MySQL 8 passwords

Preface Readers who are familiar with MySQL may f...

Flex layout realizes the layout mode of upper and lower fixed and middle sliding

This article mainly introduces the layout method ...

Some conclusions on the design of portal website focus pictures

Focus images are a way of presenting content that ...