{"id":247,"date":"2010-07-14T16:08:13","date_gmt":"2010-07-14T16:08:13","guid":{"rendered":"http:\/\/poojanwagh.opalstacked.com\/techblog\/?p=247"},"modified":"2010-07-14T16:08:13","modified_gmt":"2010-07-14T16:08:13","slug":"using-linux-command-lockfile-in-bash-scripts","status":"publish","type":"post","link":"https:\/\/tech.poojanblog.com\/blog\/unix-linux\/using-linux-command-lockfile-in-bash-scripts\/","title":{"rendered":"Using Linux command lockfile in bash scripts"},"content":{"rendered":"<p>I needed to create a script that runs a regression test. I intended to schedule this with a cron job. However, I found that sometimes, my cron jobs would collide with each other.<\/p>\n<p>So, I looked to Linux&#8217; <code>lockfile<\/code> command to create a file lock as a semaphore.<\/p>\n<p>Here&#8217;s the incantation:<\/p>\n<p>[cc_bash]<\/p>\n<p># if we&#8217;re already running, exit<br \/>\nif ! lockfile -r 0 regression.lock; then<br \/>\n   echo &#8220;regression.sh already running in area $1&#8221;<br \/>\n   exit 1<br \/>\nfi<br \/>\n.\/tools\/regression.py<br \/>\nrm -f regression.lock<\/p>\n<p>[\/cc_bash]<br \/>\nIn this case, lockfile is set with a retry count of 0 <code>-r 0<\/code>. However, I can chose to make this a retry of 1000 (for example). <\/p>\n<p>One can specify a timeout period (for example 60 seconds) with <code>-s 60<\/code>. Note, however, that this timeout period starts when the lockfile was last modified\/created, and it forces the removal of the lockfile. This helps prevent zombie processes that were killed before they could remove their lockfile.<\/p>\n<div class='wp_likes' id='wp_likes_post-247'><a class='like' href=\"javascript:wp_likes.like(247);\" title='' ><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/tech.poojanblog.com\/blog\/wp-content\/plugins\/wp-likes\/images\/like.png\" alt='' border='0'\/><\/a><span class='text'>Be the first to like.<\/span><\/p>\n<div class='like' ><a href=\"javascript:wp_likes.like(247);\">Like<\/a><\/div>\n<div class='unlike' ><a href=\"javascript:wp_likes.unlike(247);\">Unlike<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I needed to create a script that runs a regression test. I intended to schedule this with a cron job. However, I found that sometimes, my cron jobs would collide with each other. So, I looked to Linux&#8217; command to create a file lock as a semaphore. Here&#8217;s the incantation: [cc_bash] # if we&#8217;re already [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[100,99],"class_list":["post-247","post","type-post","status-publish","format-standard","hentry","category-unix-linux","tag-bash","tag-lockfile"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/comments?post=247"}],"version-history":[{"count":6,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/posts\/247\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/media?parent=247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/categories?post=247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.poojanblog.com\/blog\/wp-json\/wp\/v2\/tags?post=247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}