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?
Asked
Active
Viewed 131 times
0
-
1[What are you trying to accomplish with these two loops?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – bishop Dec 29 '16 at 18:56
-
I am trying to have one write to a file, and the other one display the file with Zenity. See the linked question. – Feldspar15523 Dec 29 '16 at 19:00
-
1. Why do they have to run simultaneously? 2. You said that methods you tried didn't work, but didn't say what about them didn't work. – Carcigenicate Dec 29 '16 at 19:05
-
I could not pass values between them. – Feldspar15523 Dec 29 '16 at 19:50
1 Answers
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