Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

7 Answers 7

I found an answer :

$ cmd="..some command..."; for i in $(seq X); do $cmd; sleep Y; done

X number of times to repeat.

Y time to wait to repeat.

Example :

$ cmd="echo"; for i in $(seq 5); do $cmd "This is echo number: $i"; sleep 1;done
share|improve this answer
    
Why is this an improvement? You just added an extra, needless, step by saving the command as a variable. The only things this does is i) make it longer to type ii) forces you to use only simple commands, no pipes or redirects etc. –  terdon Jul 3 at 11:52

You can create your own repeat command doing the following steps; credits here:

First, open your .bash_aliases file:

$ xdg-open ~/.bash-aliases

Second, paste these lines at the bottom of the file and save:

repeat() {
n=$1
shift
while [ $(( n -= 1 )) -ge 0 ]
do
    "$@"
done
}

Third, either close and open again your terminal, or type:

$ source ~/.bash_aliases

Et voilà ! You can now use it like this:

$ repeat 5 echo Hello World !!!

or

$ repeat 5 ./myscript.sh
share|improve this answer

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 :

$ inotifywait .

Then in 2nd terminal, any command that affects the current directory,

$ touch newfile

Then in original terminal inotifywait will wake up and report the event

./ CREATE newfile2

Or in a loop

$ while true ; do inotifywait . ; done
Setting up watches.  
Watches established.
./ OPEN newfile2
Setting up watches.  
Watches established.
./ OPEN newfile2
Setting up watches.  
Watches established.
./ DELETE newfile
Setting up watches.  
Watches established.
./ CREATE,ISDIR newdir
Setting up watches.  
Watches established.
share|improve this answer
    
the user told you , no script , and maybe he dont want to monitor anything –  nux Mar 6 at 16:36
2  
I didn't tell him to write a script, I suggested that if they are looping inorder to watch for particular filesystem event, then inotifywait is useful, and uses less resources than repeating a command. I often run several commands on a command line eg grep something InALogFile|less is that a script ? –  X Tian Mar 6 at 16:43
    
its a good answer , try to edit it to look more simple . –  nux Mar 6 at 16:45
2  
What could be simpler than . I can't leave out the command. –  X Tian Mar 6 at 16:48
    
Thanks @XTian, a great command. I also now saw in the man page that you can add -m to continually monitor without a loop. –  yoniLavi Jul 21 at 16:06

You can use watch command , watch is used to run any designated command at regular intervals .

Open Terminal and type :

watch -n x <your command>

change x to be the time you want .

For more help using the watch command and its options, run man 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:

watch -n 60 ls -l ~/Desktop
share|improve this answer
14  
+1 but be careful when using expansions. For example, try the difference between watch -n 1 'echo $COLUMNS' and watch -n 1 echo $COLUMNS when resizing your terminal - the former is expanded every second, but the latter is expanded only once before watch starts. –  l0b0 Mar 6 at 16:06
2  
down voters should at least leave a command to edit my answer , if i am wrong –  nux Mar 6 at 16:54
4  
I know I'm a pedant and a grammar Nazi, but the language (mis)use on SE is driving me nuts. –  Agi Hammerthief Mar 6 at 17:36
6  
+1 for answering the question even if your English is not 100% perfect. –  dotancohen Mar 7 at 12:02
1  
comments removed Please keep comment discussions on-topic to the post at hand. Discussions about other topics can be done in chat. Rude or offensive comments will be removed. –  Seth Mar 8 at 17:16

Just wanted to pitch in to souravc and nux's answers:

  1. While watch will work perfectly on Ubuntu, you might want to avoid that if you want your "Unix-fu" - on FreeBSD for example, watch is a command to "snoop on another tty line".
  2. while true; do command; sleep SECONDS; done also has a caveat - your command might be harder to kill using CTR+C. You might want to prefer while sleep SECONDS; do command; done - 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.
share|improve this answer
1  
Hopefully it's acceptable as an answer instead of a comment - I wanted to show another solution here and commenting would actually give me less attention. –  d33tah Mar 8 at 15:52
    
+1 from me , for your information –  nux Mar 8 at 15:56

You can also use this command in terminal, apart from nux's answer :

while true; do <your_command>; sleep <interval_in_seconds>; done

Example

while true; do ls; sleep 2; done

This command will print output of ls at an interval of 2 sec.

Use Ctrl+C to stop the process.

There is few drawbacks of watch

  • It can not use any aliased commands.
  • If the output of any command is quite long, scrolling does not work properly.
  • There is some trouble to set maximum time interval beyond certain value.
  • watch will interpret ANSI color sequences passing escape characters using -c or --color option. For example output of pygmentize will work but it will fail for ls --color=auto.

In the above circumstances this may appear as a better option.

share|improve this answer
    
watch exists for that, this is a bit useless I would say –  Bruno Pereira Mar 6 at 16:36
8  
I am not claiming this answer is to be used at first place. watch is good in most cases. That is why I mentioned "apart from nux's answer" at the beginning. But there are few problems with watch for example One can not use any aliased commands with watch. Take for example ll which is aliased to ls -laF but can not be used with watch. Also in case if the output of any command is quite long you will be in trouble in scrolling using watch. In these few special cases this answer may appear a better option. –  souravc Mar 6 at 17:25
    
@souravc My version of watch at least allows the -c or --color options for colorized output. –  Istvan Chung Mar 7 at 1:57
    
@IstvanChung I was saying without adding this --color option. with --color option watch will interpret ANSI color sequences passing escape characters.for example output of pygmentize but it will fail in case of ls --color=auto. I should have been mentioned that. thanks anyway. –  souravc Mar 7 at 2:15
3  
while sleep x is better - it's easier to kill. –  d33tah Mar 8 at 15:43

Sounds like the ideal task for the cron daemon which allows for running periodic commands. Run the crontab -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:

The time and date fields are:

       field          allowed values
       -----          --------------
       minute         0-59
       hour           0-23
       day of month   1-31
       month          1-12 (or names, see below)
       day of week    0-7 (0 or 7 is Sunday, or use names)

For example, if you would like to run a Python script on every Tuesday, 11 AM:

0 11 * * 1 python ~/yourscript.py

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 with crontab -l):

# Creates a temporary directory for ~/.distcc at boot
@reboot ln -sfn "$(mktemp -d "/tmp/distcc.XXXXXXXX")" "$HOME/.distcc"
share|improve this answer
    
The question asks how to run something periodically in the terminal. cron runs behind the scenes rather than in the terminal –  northern-bradley Dec 15 at 19:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.