0

I am wondering how to run too loops at the same time. This is kind of like my other question, How to exit a Subshell. I am trying to have two loops running at the same time, and have them be able to share variables. I tried using a subshell (which clearly failed), tried backgrounding the first loop and running the second one, which didn't work. What other options do I have?

Community
  • 1
  • 1
Feldspar15523
  • 165
  • 2
  • 11

1 Answers1

1

One way to achieve this would be to put the loops in different scripts and call them independently, such as:

nohup /folder/myLoop1 > /dev/null 2>&1 &
nohup /folder/myLoop2 > /dev/null 2>&1 &

As for sharing variables, one way to do it would be to store the variable on a temporary file, which then can be accessed by all scripts:

echo "$myVar" > /tmp/loopVarHolder
myVar="$(cat /tmp/loopVarHolder)"

Finally, you could kill (if you need/want) one loop (say) myLoop2 when myLoop1 finishes, by adding the following (or similar) at the end of the scrip myloop1:

killall myLoop2 > /dev/null 2>&1

Note: using nohup on the terminal (not on a script) can hold the prompt and create a file nohup.out somewhere if you don't use a redirection such as > /dev/null 2>&1 when invoking it.

Jamil Said
  • 2,033
  • 3
  • 15
  • 18