0

I have 3 scripts that send some metrics:

  • script1.sh takes ~1s to execute
  • script2.sh takes ~10s to execute
  • script3.sh takes ~2min to execute

I want to send metrics from script1.sh and script2.sh every ~30s and from script3.sh as soon as possible (there can be only one instance of this script running at a time). In other words, to parallelize 3 while loops:

#!/bin/sh

while true; do
    ./script1.sh
    sleep 30
done

while true; do
    ./script2.sh
    sleep 20
done

while true; do
    ./script3.sh
done

How do I achieve this?

1

Untested, but try running each loop in a separate backgrounded subshell

( while true; do ./script1.sh; sleep 30; done ) &
( while true; do ./script2.sh; sleep 20; done ) &
( while true; do ./script3.sh;           done ) &
wait
1
  • Thanks, I didn't think about spawning subshells. Will remember this one :)
    – SantaXL
    Nov 12 '20 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.