{"id":366,"date":"2024-10-01T12:57:01","date_gmt":"2024-10-01T12:57:01","guid":{"rendered":"https:\/\/wp.sheepchen.net\/?p=366"},"modified":"2025-04-13T22:40:48","modified_gmt":"2025-04-13T22:40:48","slug":"threads-in-bash-2","status":"publish","type":"post","link":"https:\/\/wp.sheepchen.net\/?p=366","title":{"rendered":"Threads in bash 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">you want threads in bash and know exactly when they finish and in which state?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nthreads=()\nf_getThreadKeyword(){ for i in ${threads&#91;@]}; do &#91;&#91; $(echo $i|cut -d':' -f2) == $1 ]] &amp;&amp; echo $(echo $i|cut -d':' -f1); done; }  # returns the keyword for a given pid\nf_addThread(){ keyword=$1; pid=$2; threads+=( \"$keyword:$pid\" ); echo \"added thread $keyword ($pid)\"; }  # adds a thread to the array\nf_removeThread(){ pid=$1; threads=( ${threads&#91;@]\/$(f_getThreadKeyword $pid):$pid} ); }  # removes a thread to the array\nf_getThreadPids(){ echo ${threads&#91;@]}|tr ' ' '\\n'|cut -d':' -f2|tr '\\n' ' '; }   # returns all pids\n\n\nsleep 3 &amp;\nf_addThread first $!\n\nsleep 10 &amp;\nf_addThread second $!\n\nsleep 5 &amp;\nf_addThread third $!\n\n\nwhile &#91;&#91; $(jobs|grep -iE 'l\u00e4uft|running' -c) -gt 0 ]]; do  # there are running background jobs\n  wait -p next -n $(f_getThreadPids)\n  err=$?\n  echo pid $next exited with $err \\($(f_getThreadKeyword $next)\\)\n  f_removeThread $next\ndone\n\necho done<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>added thread first (1429283)\nadded thread second (1429285)\nadded thread third (1429286)\npid 1429283 exited with 0 (first)\npid 1429285 exited with 143 (second)\npid 1429286 exited with 0 (third)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(I killed the second thread from another console)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>you want threads in bash and know exactly when they finish and in which state? Output: (I killed the second thread from another console)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[6,16],"tags":[],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-linux","category-programming"],"_links":{"self":[{"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts\/366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=366"}],"version-history":[{"count":2,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts\/366\/revisions"}],"predecessor-version":[{"id":369,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts\/366\/revisions\/369"}],"wp:attachment":[{"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}