Differences
This shows you the differences between two versions of the page.
bash_script_multithreading [20.07.2015 12:29] – created Pascal Suter | bash_script_multithreading [20.07.2015 12:49] (current) – Pascal Suter | ||
---|---|---|---|
Line 70: | Line 70: | ||
- however, if the main process is still around and you hit ctrl+c, it will kill also all its sub processes.. if the main process is not around anymore, ctrl+c will have no effect on the sub processes.. | - however, if the main process is still around and you hit ctrl+c, it will kill also all its sub processes.. if the main process is not around anymore, ctrl+c will have no effect on the sub processes.. | ||
- a variable that is set before the function is run and then changed while the funciton is running will remain its original value inside of the function.. that's why in our output, '' | - a variable that is set before the function is run and then changed while the funciton is running will remain its original value inside of the function.. that's why in our output, '' | ||
+ | |||
+ | ===== wait for subprocesses ===== | ||
+ | if you want to be able to use ctrl+c to abort the execution of all children even after the main process has completed, you need to keep it runnig for as long as the child processes are running.. my solution looks like this: | ||
+ | < | ||
+ | self=`basename " | ||
+ | children=`ps aux | grep " | ||
+ | while [ `echo " | ||
+ | sleep 1 | ||
+ | echo "still have `echo " | ||
+ | echo " | ||
+ | children=`ps aux | grep " | ||
+ | done | ||
+ | </ | ||
+ | that's the verbouse version that helped me to understand that i will always see 2 processes when i populate my $children variable through an external command.. | ||
+ | |||
+ | the more compact version is this: | ||
+ | < | ||
+ | self=`basename " | ||
+ | children=`ps aux | grep " | ||
+ | while [ $children -gt 2 ]; do | ||
+ | sleep 1 | ||
+ | echo "still have $children processes, keep waiting: " | ||
+ | children=`ps aux | grep " | ||
+ | done | ||
+ | </ |