Repeat command automatically in Linux
Is it possible in Linux command line to have a command repeat every n seconds?
Say, I have an import running, and I am doing
to check if the file size is increasing. I would like to have a command to have this repeat automatically.
Watch every 5 seconds ...
watch -n 5 ls -l
If you wish to have visual confirmation of changes, append
--differences prior to the
According to the OSX man page, there's also
The --cumulative option makes highlighting "sticky", presenting a running display of all positions that have ever changed. The -t or --no-title option turns off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.
Linux/Unix man page can be found here
Read more... Read less...
while true; do sleep 5 ls -l done
"watch" does not allow fractions of a second in Busybox, while "sleep" does. If that matters to you, try this:
while true; do ls -l; sleep .5; done
sleep already returns
0. As such, I'm using:
while sleep 3 ; do ls -l ; done
This is a tiny bit shorter than mikhail's solution. A minor drawback is that it sleeps before running the target command for the first time.
If the command contains some special characters such as pipes and quotes, the command needs to be padded with quotes. For example, to repeat
ls -l | grep "txt", the watch command should be:
watch -n 5 'ls -l | grep "txt"'
Running commands periodically without cron is possible when we go with
As a command:
while true ; do command ; sleep 100 ; done & [ ex: # while true; do echo `date` ; sleep 2 ; done & ]
while true do echo "Hello World" sleep 100 done &
Do not forget the last
& as it will put your loop in the background. But you need to find the process id with command "ps -ef | grep your_script" then you need to kill it. So kindly add the '&' when you running the script.
# ./while_check.sh &
Here is the same loop as a script. Create file "while_check.sh" and put this in it:
#!/bin/bash while true; do echo "Hello World" # Substitute this line for whatever command you want. sleep 100 done
Then run it by typing
bash ./while_check.sh &