1

I have a 10 second interval. I want to run a command in a shell, then wait to complete the 10 sec interval. If the commands takes more than 10 seconds, its OK.

I am looking for an equivalent to "foo" function below :

>> foo 10 python myscript.py
  • If myscript.py takes 2 seconds, the shell should wait 8 seconds afterwhile.
  • If myscript.py takes 5 seconds, the shell should wait 5 seconds afterwhile.
  • If myscript.py takes 12 seconds, the shell should not wait. It should not timeout myscript.py either !

Does this "foo" function exist ?

Vincent
  • 1,534
  • 3
  • 20
  • 42

1 Answers1

0

Just run both sleep 10 and your script in the background, then wait for both to complete.

python myscript.py &
sleep 10 &
wait

You can define a shell function to wrap simple commands (a command name and its arguments; no pipes, redirections, && and ||, etc.)

foo () {
    t=$1
    shift
    "$@" &
    sleep "$t" &
    wait
}

foo 10 python myscript.py
chepner
  • 497,756
  • 71
  • 530
  • 681