LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Script -- run two processes at once? (https://www.linuxquestions.org/questions/programming-9/bash-script-run-two-processes-at-once-608643/)

1veedo 12-22-2007 08:19 PM

Bash Script -- run two processes at once?
 
I think this might be forking but I read the wiki and it says a fork is a copy of the parent, which isn't really what I want...

Basically I want two chunks of code running at the same time. I'm sure it's something really simple -- eg kind of like how there's process1 && process2. Or even better would be the ability to directly share variables in the same bash script --

#!/bin/bash

chunk1{

}
chunk 2{

}

Either way works.

Uncle_Theodore 12-22-2007 08:30 PM

Something like this?

Code:

#!/bin/bash

f() { for((i=0;$i<10;i++)); do echo $i; sleep 1; done; }
g() { for((j=100;$j>90;j--)); do echo $j; sleep 1; done; }

f &
g

teddy@toshiba~$ ./two.sh
0
100
99
1
98
2
3
97
4
96
5
95
94
6
93
7
92
8
91
9

1veedo 12-22-2007 09:40 PM

Yeah, this is what I was looking for. & just puts a process in the background (forgot all about that trick). Is this the same thing as forking or different?

It's weird though I had this bash script and arranged the code in the two functions. I have some image processes running in f() and when I run f& (eg f & g) I get these png errors. But if I run f (eg g & f) it doesn't error. Why would some programs need to be ran "directly"? Why cant they be put in the background?

edit -- Hm for some reason it works now. I never touched the code. Weird.

Thanks for the info that was exactly what I was looking for.


All times are GMT -5. The time now is 05:36 PM.