1. Home
  2. Linux
  3. General
  4. How To Run A Command For A Specific Time In Linux

How To Run A Command For A Specific Time In Linux

How To Run A Command For A Specific Time In Linux
How To Run A Command For A Specific Time In Linux

The other day I was moving a big file using ‘rsync’ to another system on my local area network. Because it’s very large, it took around 20 minutes to finish. Nobody wants to wait that long, and we don’t want to terminate the process by pressing CTRL+C either.
So, in this tutorial, we will teach you how to run a command for a specific time and kill it automatically after the time is up in Unix-like operating systems.
Run A Command For A Specific Time In Linux
There are two methods to do this.

Method 1 – Using the ‘timeout’ command

The most common method is using the ‘timeout’ command.
If you do not know, the ‘timeout’ command should effectively limit the absolute execution time of a process. The ‘timeout’ command is part of the GNU coreutils package, so it arrives pre-installed in every GNU/Linux system.
If you want to execute a command for only 5 seconds and kill it afterwards, you will need to use the following.

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

For instance, the command below will terminate after 10 seconds.

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

You do not need to specify the suffix ‘s’ for seconds. The command below is the same as the one above.

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

The other available suffixes are:

  • ‘m’ for minutes
  • ‘h’ for hours
  • ‘d’ for days

If you execute the command tail ‘–f /var/log/pacman.log’, it will continuously run until you manually end it by pressing CTRL+C. However, if you execute it along with the ‘timeout’ command, it will be killed automatically after the given time interval.
If the command is still running after the time out, you could send a kill signal as shown below.

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

In a situation like this, if the tail command is still running after 10 seconds, the ‘timeout’ command will send it a kill signal after 20 seconds and end it.
For more details, check the man pages.

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

Often, a specific program might take a while to finish and will end up freezing your system.
In these cases, you can use this trick to eliminate the process automatically after a particular time.

Method 2 – Using the ‘Timelimit’ program

The ‘timelimit’ utility runs a given command with the supplied arguments and terminates the spawned process after a given time with ‘ag iven’ 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 a bunch more options. You may pass a number of arguments such as ‘killsig’, ‘warnsig’, ‘killtime’, ‘warntime’, and more. It is available in the default repositories of Debian-based systems. You can install it with the command below.

$ sudo apt-get install timelimit

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’, and more.
For different distributions, download the source from here and manually install it. Once you’ve installed the ‘timelimit’ program, execute the command below for a specific amount of time, for example10 seconds.

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

If you execute ‘timelimit’ without any arguments, it will use the default values: ‘warntime=3600 seconds’, ‘warnsig=15’, ‘killtime=120’, ‘killsig=9’. For more information, refer to the man pages and the project’s website given at the end of this tutorial.

$ man timelimit

I hope this was useful. Stay tuned!

Updated on January 9, 2019

Was this article helpful?

Related Articles

Leave a Comment

[apsl-login-lite login_text='Please login with a social account']