Home Linux Tips & Tricks How To Run A Command For A Specific Time In Linux

How To Run A Command For A Specific Time In Linux

By sk
Published: Updated: 34.8K views

The other day I was transferring a large file using rsync to another system on my local area network. Since it is very big file, It took around 20 minutes to complete. I don't want to wait that much longer, and also I don't want to manually terminate the process by pressing CTRL+C either. I was just wondering if there are any easy ways to run a command for a specific time and kill it automatically once the time is out in Linux and Unix-like operating systems - hence this post. Read on.

Run A Command For A Specific Time In Linux

We can do this in two methods using timeout and timelimit utilties.

Method 1 - Using "timeout" Command

The most common method is using timeout command for this purpose. For those who don't know, the timeout command will effectively limit the absolute execution time of a process. The timeout command is part of the GNU coreutils package, so it comes pre-installed in all GNU/Linux systems.

Let us say, you want to run a command for only a specific time, and then kill it automatically once the time is passed. To do so, 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
Run A Command For A Specific Time Using Timeout Command
Run A Command For A Specific Time Using Timeout Command

You also don't have to specify the suffix "s" for seconds. The following command is same as 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 run this tail -f /var/log/pacman.log command, it will keep running until you manually end it by pressing CTRL+C. However, if you run it along with timeout command, it will be killed automatically after the given time interval.

Just in case if the command is still running even after the time out, you can send a kill signal like below.

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

In this case, if you the tail command still running after 10 seconds, the timeout command will send it a kill signal after 20 seconds and end it.

The Tmeout command can be especially useful when troubleshooting hardware issues. For instance, run the following command to display all messages from the kernel ring buffer, but for only 10 seconds.

$ timeout 10 dmesg -w

For more details, check the man pages.

$ man timeout

Sometimes, a particular program might take long time to complete and end up freezing your system. In such cases, you can use this trick to end the process automatically after a particular time.

Also, consider using Cpulimit , a simple application to limit the CPU usage of a process. For more details, check the following link.

Method 2 - Using "Timelimit" Program

The Timelimit utility executes a given command with the supplied arguments and terminates the spawned process after a given time with a given 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 more options. You can pass number of arguments such as 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 the AUR. So, you can install it using any AUR helper programs such as Pacaur, Packer, Yay, Yaourt etc.

For other distributions, download the source from here and manually install it.

After installing Timelimit program, run the following command for a specific time, for example 10 seconds:

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

If you run timelimit without any arguments, it will use the default values: warntime=3600 seconds, warnsig=15, killtime=120, killsig=9.

For more details, refer the man pages and the project's website given at the end of this guide.

$ man timelimit

And, that's all for today. I hope this was useful. More good stuffs to come. Stay tuned!

Cheers!

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More