How to create Multiple Threads in Bash Shell Script -


this question has answer here:

i have array of arguments utilized such in command shell script. want able this

./runtests.sh -b firefox,chrome,ie 

where each command here start separate thread (currently multithreading opening multiple terminals , starting commands there)

i have pushed entered commands array:

if [[ $browser == *","* ]];     ifs=',' read -ra browserarray <<< "$browser" fi 

now have start separate thread (or process) while looping through array. can guide me in right direction? guess in sudo code like

for (( c=0; c<${#browserarray}; c++ ))         starttests & 

am on right track?

that's not thread, background process. similar but:

so, can threads , light weight processes same.

the main difference between light weight process (lwp) , normal process lwps share same address space , other resources open files etc. resources shared these processes considered light weight compared other normal processes , hence name light weight processes.

nb: redordered clarity

what linux processes, threads, light weight processes, , process state

you can see running background process using jobs command. e.g.:

nick@nick-lt:~/test/npm-test$ sleep 10000 & [1] 23648 nick@nick-lt:~/test/npm-test$ jobs [1]+  running     

you can bring them foreground using fg:

nick@nick-lt:~/test/npm-test$ fg 1 sleep 1000 

where cursor wait until sleep time has elapsed. can pause job when it's in foreground (as in scenario after fg 1) pressing ctrl-z (sigtstp), gives this:

[1]+  stopped                 sleep 1000 

and resume typing:

bg 1    # resumes in background fg 1    # resumes in foreground 

and can kill pressing ctrl-c (sigint) when it's in foreground, ends process, or through using kill command % affix jobs id:

kill %1 # or kill <pid> 

onto implementation:

browsers=  in "${@}";     case $i in         -b)             shift            browsers="$1"            ;;          *)            ;;     esac done  ifs=',' read -r -a splitbrowsers <<< "$browsers"  browser in "${splitbrowsers[@]}"     echo "running ${browser}..."     $browser & done 

can called as:

./runtests.sh -b firefox,chrome,ie 

tadaaa.


Comments