Repeat a Linux Command at a Given Interval


Linux is a powerful operating system that offers a wide range of command-line tools for executing various tasks. One of tasks that often need to be performed is repeating a command at a given interval. This feature can be useful for several reasons, such as monitoring system performance, running scheduled tasks, and performing backups.

In this article, we will explore how to repeat a Linux command at a given interval. We will discuss various ways to achieve this, including using cron utility, watch command, and sleep command.

Using Cron to Repeat a Command

The cron utility is a time-based job scheduler in Unix-like operating systems, including Linux. It allows users to automate various tasks by scheduling them to run at specified intervals. To use cron utility to repeat a command, follow these steps −

  • Step 1 − Open crontab file by running following command −

$ crontab -e

This will open crontab file in your default text editor.

  • Step 2 − Add a new cron job by specifying command you want to repeat and interval at which you want it to run. For example, if you want to run command ls every 5 minutes, add following line to crontab file −

*/5 * * * * ls

The above cron expression specifies that command ls should be run every 5 minutes. five asterisks represent following fields −

  • The first field represents minute when command should run (0-59).

  • The second field represents hour when command should run (0-23).

  • The third field represents day of month when command should run (1-31).

  • The fourth field represents month when command should run (1-12).

  • The fifth field represents day of week when command should run (0-6, where 0 is Sunday).

  • Step 3 − Save and close crontab file.

The above example will run ls command every 5 minutes. You can modify cron expression to suit your needs.

Using Watch Command to Repeat a Command

The watch command is a utility that allows users to run a command repeatedly and display output on terminal in real-time. To use watch command to repeat a command, follow these steps −

  • Step 1 − Open a terminal and type following command −

$ watch -n <interval> <command>

Replace <interval> with time interval at which you want command to repeat, in seconds. Replace <command> with command you want to repeat.

For example, if you want to run top command every 5 seconds, type following command −

$ watch -n 5 top

The above command will run top command every 5 seconds and display output on terminal.

  • Step 2 − To stop watch command, press Ctrl+C.

The watch command can be useful for monitoring system performance or watching output of a long-running command.

Using Sleep Command to Repeat a Command

The sleep command is a utility that causes shell to wait for a specified time before executing next command. To use sleep command to repeat a command, follow these steps −

  • Step 1 − Open a terminal and type following command −

$ while true; do <command>; sleep <interval>; done

Replace <command> with command you want to repeat. Replace <interval> with time interval at which you want command to repeat, in seconds.

For example, if you want to run date command every 5 seconds, type following command −

$ while true; do date; sleep 5; done

The above command will run date command every 5 seconds.

  • Step 2 − To stop command, press Ctrl+C.

The sleep command can be useful for performing backups or running other scheduled tasks.

Here are some additional tips and examples for repeating Linux commands at a given interval −

Using at Command

The at command is a utility that allows you to schedule a command to run at a specific time. To use at command to repeat a command at a given interval, follow these steps −

  • Step 1 − Open a terminal and type following command −

$ echo "<command>" | at now + <interval>

Replace <command> with command you want to repeat. Replace <interval> with time interval at which you want command to repeat, in minutes or hours.

For example, if you want to run ls command every 30 minutes, type following command −

$ echo "ls" | at now + 30 minutes

The above command will run ls command every 30 minutes.

  • Step 2 − To stop command, use atq command to list jobs and then use atrm command to remove job.

Using a Bash Script

You can also use a bash script to repeat a command at a given interval. To do this, follow these steps −

  • Step 1 − Open a text editor and create a new file. Type following code −

#!/bin/bash
while true
do
   <command>
   sleep <interval>
done

Replace <command> with command you want to repeat. Replace <interval> with time interval at which you want command to repeat, in seconds.

For example, if you want to run date command every 5 seconds, type following code −

#!/bin/bash

while true
do
   date
   sleep 5
done
  • Step 2 − Save file with a .sh extension, for example, repeat_command.sh.

  • Step 3 − Make script executable by running following command −

$ chmod +x repeat_command.sh
  • Step 4 − Run script by typing following command −

$ ./repeat_command.sh

The above command will run bash script, which will run specified command at given interval.

Combining Commands

You can also combine multiple commands and run them at a given interval. For example, to run free command and df command every 5 minutes, type following command −

$ while true; do free; df; sleep 300; done

The above command will run free command and df command every 5 minutes.

Conclusion

Repeating a Linux command at a given interval can be useful for a wide range of tasks, including monitoring system performance, running scheduled tasks, and performing backups. In this article, we explored various ways to achieve this, including using cron utility, watch command, and sleep command. Each of these methods has its advantages and disadvantages, so choose one that best suits your needs.

Remember to always use caution when running commands in terminal, and make sure you understand what each command does before executing it. With knowledge gained from this article, you can now repeat Linux commands at a given interval with ease.

Updated on: 14-Mar-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements