Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 task that often needs 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 automated backups.
In this article, we will explore how to repeat a Linux command at a given interval. We will discuss various methods to achieve this, including using the cron utility, watch command, sleep command, and other scheduling tools.
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 the cron utility to repeat a command, follow these steps
Step 1 Open the crontab file by running the following command
$ crontab -e
This will open the crontab file in your default text editor.
Step 2 Add a new cron job by specifying the command you want to repeat and the interval at which you want it to run. For example, if you want to run the ls command every 5 minutes, add the following line to the crontab file
*/5 * * * * ls
The above cron expression specifies that the ls command should be run every 5 minutes. The five fields represent the following
The first field represents the minute when the command should run (0-59).
The second field represents the hour when the command should run (0-23).
The third field represents the day of the month when the command should run (1-31).
The fourth field represents the month when the command should run (1-12).
The fifth field represents the day of the week when the command should run (0-6, where 0 is Sunday).
Step 3 Save and close the crontab file.
The above example will run the ls command every 5 minutes. You can modify the 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 the output on the terminal in real-time. To use the watch command to repeat a command, follow these steps
Step 1 Open a terminal and type the following command
$ watch -n <interval> <command>
Replace <interval> with the time interval at which you want the command to repeat, in seconds. Replace <command> with the command you want to repeat.
For example, if you want to run the top command every 5 seconds, type the following command
$ watch -n 5 top
The above command will run the top command every 5 seconds and display the output on the terminal.
Step 2 To stop the watch command, press Ctrl+C.
The watch command can be useful for monitoring system performance or watching the output of a long-running command.
Using Sleep Command to Repeat a Command
The sleep command is a utility that causes the shell to wait for a specified time before executing the next command. To use the sleep command to repeat a command, follow these steps
Step 1 Open a terminal and type the following command
$ while true; do <command>; sleep <interval>; done
Replace <command> with the command you want to repeat. Replace <interval> with the time interval at which you want the command to repeat, in seconds.
For example, if you want to run the date command every 5 seconds, type the following command
$ while true; do date; sleep 5; done
The above command will run the date command every 5 seconds.
Step 2 To stop the command, press Ctrl+C.
The sleep command can be useful for performing backups or running other scheduled tasks.
Using at Command
The at command is a utility that allows you to schedule a command to run at a specific time. While primarily designed for one-time scheduling, it can be combined with other techniques for repeated execution.
Step 1 Open a terminal and type the following command
$ echo "<command>" | at now + <interval>
Replace <command> with the command you want to run. Replace <interval> with the time interval, such as "30 minutes" or "2 hours".
For example, if you want to run the ls command after 30 minutes, type the following command
$ echo "ls" | at now + 30 minutes
Step 2 To manage scheduled jobs, use atq to list jobs and atrm to remove a job.
Using a Bash Script
You can also create 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 with the following code
#!/bin/bash while true do <command> sleep <interval> done
Replace <command> with the command you want to repeat. Replace <interval> with the time interval in seconds.
For example, to run the date command every 5 seconds
#!/bin/bash while true do date sleep 5 done
Step 2 Save the file with a .sh extension, for example, repeat_command.sh.
Step 3 Make the script executable by running the following command
$ chmod +x repeat_command.sh
Step 4 Run the script by typing the following command
$ ./repeat_command.sh
Comparison of Methods
| Method | Best Use Case | Advantages | Disadvantages |
|---|---|---|---|
| Cron | Background scheduled tasks | Persistent across reboots, flexible scheduling | Minimum interval is 1 minute |
| Watch | Real-time monitoring | Live output display, easy to use | Terminal-based only, no background execution |
| Sleep loop | Simple repeated execution | Flexible intervals, works in any shell | Stops when terminal closes |
| Bash script | Complex repeated tasks | Reusable, can include logic | Requires script creation and management |
Combining Commands
You can also combine multiple commands and run them at a given interval. For example, to run the free and df commands every 5 minutes
$ while true; do free; df; sleep 300; done
The above command will run both free and df commands every 5 minutes (300 seconds).
Conclusion
Repeating a Linux command at a given interval is essential for system monitoring, automation, and scheduled tasks. Each method discussed cron, watch, sleep loops, and bash scripts serves different purposes and offers unique advantages. Choose the method that best fits your specific requirements and environment.
