{"id":254,"date":"2023-10-20T13:53:40","date_gmt":"2023-10-20T13:53:40","guid":{"rendered":"https:\/\/wp.sheepchen.net\/?p=254"},"modified":"2023-10-24T09:30:01","modified_gmt":"2023-10-24T09:30:01","slug":"threads-in-bash","status":"publish","type":"post","link":"https:\/\/wp.sheepchen.net\/?p=254","title":{"rendered":"threads in bash"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Here an easy script for using threads in bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nthreads=4\n\nthread(){\n  echo \"thread $$ started with param $1\"\n  sleep 10  # do stuff\n}\n\nselfcount(){ echo $(( $(ps -ef|grep -v grep|grep -c $0) - 3 )); }\n\nfor i in {1..100}; do   # worktasks\n  while &#91;&#91; $(selfcount) -ge $threads ]]; do\n    sleep 0.1\n  done\n  thread $i &amp;   # call thread in background\n  sleep 0.1\ndone\n\nwhile &#91;&#91; $(selfcount) > 0 ]]; do sleep 0.1; done # wait for the last threads to finish<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here an easy script for using threads in bash<\/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],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts\/254","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=254"}],"version-history":[{"count":1,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.sheepchen.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}