As a Linux user, you often need to run some commands repeatedly after a certain period of time. Sometimes, you need a command to run each hour or in an interval of X seconds. This is commonly used by system admins but even if you are a beginner, you can use it to automate tasks, synchronize files or schedule updates, etc. There are many methods to do it which are quite easy and direct.
- Using Cron command
- Using loops (for loop, while loop)
- Watch command
Run Commands in Linux Every Few Seconds
The cron command cannot be used to run commands every X seconds, and using loops is not precise. The watch command is easy to use. In this article, We will discuss all three methods in detail.
1. Using Cron command
Each user can have a crontab where we can create and modify tasks. But, Cron can only be used for a minimum interval of one minute i.e. if you want to run commands every X seconds, Cron can’t be used.
Open a terminal by pressing Ctrl+Alt+T. Enter the following command to open the config file for the current user:
crontab -e
Add a task entry in the following format:
* * * * * / directory/ command
where the first five dots represent time i.e. minute, hour, day of the month, month, and day of the week. For Example:
* * * * * echo "hello world" > /home/sid/Desktop/abc.sh
To edit crontab for other users, Enter the following command:
crontab -u <username>
To list the scheduled jobs, enter the following command:
crontab -l
To delete a crontab, enter the following command:
crontab -r
So, setting up cron jobs is easy but it only runs commands at a minimum interval of one minute.
2. Using watch command
The watch command can be used to repeat a command from every two seconds to every day, month, or year. Watch command displays the output in the terminal until we manually stop it either by pressing Ctrl+Alt+T or by rebooting the system. To use the watch command, Open a terminal and enter the following command:
watch uptime
By default, the output is displayed in the terminal. To save the output in a text file, enter the following command:
watch uptime > /home/sid/Desktop/test.txt
The output will be saved in the textile test.txt until you stop it manually.
By default, it displays the output every 2 seconds. To set a different time interval, enter the following command:
watch -n 30 uptime
This will run the uptime command in an interval of 30 seconds. To know more about the watch command, You can refer to the manpage or through this link.
3. Using sleep command
The sleep command can also be used along with loops like while loop or for loop to run a command repeatedly or periodically. Open a terminal and using for loop, execute the following command:
for i in {1..8}; do echo -n "running the command $i time"; sleep 3; done
The above command will display the output 8 times with a time interval of 3 seconds. You can also use while loop instead of for loop as shown below.
while true; do echo -n "this will run every 2 seconds" ; sleep 2; done
Conclusion
So, we learned how to run commands in a Linux terminal periodically or repeatedly. From running a command to running a script every X seconds or every hour, everything can be done using these three methods.