Linux command to run script at intervals

11,644

Solution 1

You could try something like this:

while true; do
  python2.5 /home/me/web/gae/google_appengine/dev_appserver.py /home/me/web/gae/APPLICATION/trunk &
  sleep 10
  kill $!
done

I.e.: Loop forever (while true), start the python script in background, wait for 10 seconds (sleep 10) and kill the background process (kill $!).

Solution 2

I like ~$ watch -n sec command

i.E.

watch -n 10 ls /home/user/specialdata

watch -n 30 csync /dir/A /remote/dir/B

Solution 3

there is sleep and at if you don't like cron

echo "print after 3min again"
sleep 180  # or sleep +3m
echo "hello again, 3min passed"

Read the man pages, play with those a bit, and I think it'd be easy to build what you want, around those.

Share:
11,644
kieran
Author by

kieran

Web Developer from London

Updated on June 09, 2022

Comments

  • kieran
    kieran about 2 years

    I have this command that I run from a terminal in ubuntu

    python2.5 /home/me/web/gae/google_appengine/dev_appserver.py /home/me/web/gae/APPLICATION/trunk
    

    I need to stop this running and then restart it every 10 seconds - I can run this from a .sh file if necessary.

    What would be the best way to do this? I'd like it to all be in one script if possible so not that keen on using cron jobs to run it - surely there is some way of doing a loop with a delay in purely in a shell script?

    The closest equivalent I can think of is JavaScript's setInterval(function(),10000);

  • contrebis
    contrebis almost 9 years
    I think this assumes that the process being watched terminates, whereas the OP is running a server process that needs to be killed. But I wasn't aware of watch so thanks for the tip-off.
  • Mohit Shah
    Mohit Shah almost 5 years
    Can we give time interval in milliseconds with watch command ?
  • woky
    woky about 2 years
    That's setTimeout(), not setInterval().