How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ?
There is no need for a script, i need just a simple command to be executed in terminal.
How can I repeat a command every interval of time , so that it will allow me to run commands for checking or monitoring directories ?
There is no need for a script, i need just a simple command to be executed in terminal.
You can use
watch
command, watch is used to run any designated command at regular intervals.Open Terminal and type:
change x to be the time in seconds you want.
For more help using the
watch
command and its options, runman watch
or visit this Link.For example : the following will list, every 60s, on the same Terminal, the contents of the Desktop directory so that you can know if any changes took place:
You can also use this command in terminal, apart from nux's answer:
Example:
This command will print the output of
ls
at an interval of 2 sec.Use Ctrl+C to stop the process.
There are few drawbacks of
watch
:watch
will interpret ANSI color sequences passing escape characters using-c
or--color
option. For example output ofpygmentize
will work but it will fail forls --color=auto
.In the above circumstances this may appear as a better option.
Just wanted to pitch in to sourav c.'s and nux's answers:
While
watch
will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" to be pure. On FreeBSD for example,watch
is a command to "snoop on another tty line".while true; do command; sleep SECONDS; done
also has a caveat: your command might be harder to kill using Ctrl+C. You might prefer:It's not only shorter, but also easier to interrupt. The caveat is that it will first sleep, then run your command, so you'll need to wait some
SECONDS
before the first occurrence of the command will happen.Sounds like the ideal task for the
cron
daemon which allows for running periodic commands. Run thecrontab -e
command to start editing your user's cron configuration. Its format is documented in crontab(5). Basically you have five time-related, space-separated fields followed by a command:For example, if you would like to run a Python script on every Tuesday, 11 AM:
There are also some special names that replace the time, like
@reboot
. Very helpful if you need to create a temporary directory. From my crontab (listed withcrontab -l
):If you are monitoring the file system, then
inotifywait
is brilliant and certainly adds less load on your system.Example :
In 1st terminal type this command :
Then in 2nd terminal, any command that affects the current directory,
Then in original terminal inotifywait will wake up and report the event
Or in a loop
you can use crontab. run the command
crontab -e
and open it with your preferred text editor, then add this lineThis will run your command every 10 minutes
This will run your command every 4 hours
Another possible solution
X number of times to repeat.
Y time to wait to repeat.
Example :
You can create your own
repeat
command doing the following steps; credits here:First, open your
.bash_aliases
file:Second, paste these lines at the bottom of the file and save:
Third, either close and open again your terminal, or type:
Et voilà ! You can now use it like this:
or
Another concern with the "watch" approach proposed above is that it does display the result only when the process is done. "date;sleep 58;date" will display the 2 dates only after 59 seconds... If you start something running for 4 minutes, that display slowly multiple pages of content, you will not really see it.
On the other hand, the concern with the "while" approach is that it doesn't take the task duration into consideration.
With this, the script will run sometime every minutes, sometime might take 1m40. So even if a cron will be able to run it every minutes, here, it will not.
So to see the output on the shell as it's generated and wait for the exact request time, you need to look at the time before, and after, and loop with the while.
Something like:
This will output this:
As you can see, the command runs every minutes:
So just replace the "sleep
echo $(( ( RANDOM % 30 ) + 1 ))
" command with what ever you want and that will be run, on the terminal/shell, exactly every minute. If you want another schedule, just change the "60" seconds with what ever you need.Shorter version without the debug lines: