How do run a Unix command at a given time interval?

10,993

Solution 1

Use watch. The -n flag specifies interval in seconds, so

watch -n 300 ls

Solution 2

while true; do
    ls
    sleep 300
done

?

Solution 3

Put your script into the Crontab via

crontab -e

More information about Cron you can find at wikipedia

Solution 4

You could use crontab for example. See http://en.wikipedia.org/wiki/Cron

for example i you want to run your script every five minutes via crontab it should look something like this:

#m  h dom mon dow user command
*/5 * *   *   *   root /path/to/script
Share:
10,993
user358232
Author by

user358232

Updated on June 04, 2022

Comments

  • user358232
    user358232 about 2 years

    I want to run a Unix command (e.g. ls) at 5 minute intervals through a script.

    Explanation:

    I have a Unix script. In that script I have a command called "ls".

    I want that "ls" command to run every 5 minutes from that script.

  • carlpett
    carlpett almost 13 years
    Unless of course you want this to happen every five minutes always. That would be a job for cron, but if you just want to do it occasionally, I'd recommend using watch.
  • TheOneTeam
    TheOneTeam almost 13 years
    YES I think this is the easiest way
  • powerMicha
    powerMicha almost 13 years
    Never used watch before. Cool stuff :)
  • user358232
    user358232 almost 13 years
    I think this is the easiest way
  • Bertrand Marron
    Bertrand Marron almost 13 years
    Well most people are suggesting you to use cron and such... but if I got the question right, it looks like you already have a script and want the ls every 5 minutes within the script. So again, if I'm right, cron doesn't apply here.
  • Mike Atlas
    Mike Atlas almost 11 years
    I didn't have this on OS X, but obtained it quickly with brew install watch