{"id":553,"date":"2020-11-02T14:02:10","date_gmt":"2020-11-02T14:02:10","guid":{"rendered":"https:\/\/www.unordnung.net\/?p=553"},"modified":"2020-11-02T14:02:10","modified_gmt":"2020-11-02T14:02:10","slug":"handling-multiple-script-instances-in-bash","status":"publish","type":"post","link":"https:\/\/unordnung.net\/misc\/2020\/11\/handling-multiple-script-instances-in-bash\/","title":{"rendered":"handling multiple script instances in BASH"},"content":{"rendered":"\n<p>With flock you can implement basic job control in bash. That&#8217;s very handy when you have a script that is running in multiple instances and doing file operations. I was writing a script to return some data from a switch stack to zabbix and as zabbix does run the script for every item it gets from it&#8217;s discovery part, it is run very often.<\/p>\n\n\n<p>So the different instances tried to do file operations at the same time. After a lot of trial and error i found flock.<\/p>\n\n\n<pre class=\"wp-block-preformatted\">(\nflock 200\n# commands to be run by only one instance of the script at the same time\n) 200&gt;\/var\/lock\/mylockfile<\/pre>\n\n\n<p>that creates a lock file and before executing the part, bash checks if the lockfile is locked. Thanks at <a href=\"http:\/\/kiloroot.com\" data-type=\"URL\" data-id=\"http:\/\/kiloroot.com\">kiloroot.com<\/a>.<\/p>\n\n\n<p>Also remembering, that the linux <code>move<\/code> command is atomic, therefore very quick. It&#8217;s just changing meta-data. While <code>copy<\/code> can take a while, since it may need to handle a lot of data.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With flock you can implement basic job control in bash. That&#8217;s very handy when you have a script that is running in multiple instances and doing file operations. I was writing a script to return some data from a switch stack to zabbix and as zabbix does run the script for every item it gets &#8230; <a title=\"handling multiple script instances in BASH\" class=\"read-more\" href=\"https:\/\/unordnung.net\/misc\/2020\/11\/handling-multiple-script-instances-in-bash\/\">Read more<span class=\"screen-reader-text\">handling multiple script instances in BASH<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[11,46],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-to_remember","tag-bash","tag-linux"],"_links":{"self":[{"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/comments?post=553"}],"version-history":[{"count":0,"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"wp:attachment":[{"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unordnung.net\/misc\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}