Repeat a command every x interval of time in terminal?

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 in seconds 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

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 the output of ls at an interval of 2 sec.

Use Ctrl+C to stop the process.

There are few drawbacks of watch:

  • It cannot use any aliased commands.
  • If the output of any command is quite long, scrolling does not work properly.
  • There is some trouble to set the maximum time interval beyond a 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.