{"id":133,"date":"2010-11-11T15:20:00","date_gmt":"2010-11-11T14:20:00","guid":{"rendered":"http:\/\/eguaj.tumblr.com\/post\/1543033570"},"modified":"2011-02-02T20:25:13","modified_gmt":"2011-02-02T19:25:13","slug":"1543033570","status":"publish","type":"post","link":"https:\/\/locallost.net\/?p=133","title":{"rendered":"flock in bash script"},"content":{"rendered":"<p><strong>util-linux(-ng) flock<\/strong><\/p>\n<p>On Linux systems, there is a `flock' command (from `util-linux' or `util-linux-ng') that can be used to run a command inside a file lock\/unlock block.<\/p>\n<blockquote>\n<pre>function run_lock_flock {\n  LOCK=$1\n  shift\n  touch \"$LOCK\"\n  flock \"$LOCK\" \"$@\"\n}\n<\/pre>\n<\/blockquote>\n<p><strong>portable way with perl<\/strong><\/p>\n<p>On other Unix systems, a portable way could be to use perl\u00a0to mimic the util-linux(-ng) command shown above:<\/p>\n<blockquote>\n<pre>function run_lock_perl {\n  LOCK=$1\n  shift\n  perl -e '\n    use Fcntl\":flock\";\n    $l=shift;\n    open(L,\"&gt;&gt;\",$l)||die\"Error open: $!\\n\";flock(L,LOCK_EX)||die\"Error lock: $!\\n\";\n    system(@ARGV);$?==-1&amp;&amp;die\"Error executing command!\";\n    flock(L,LOCK_UN);close(L);\n    exit($?&gt;&gt;8)\n  ' \"$LOCK\" \"$@\"\n}<\/pre>\n<\/blockquote>\n<p><strong>autodetect flock<\/strong><\/p>\n<p>Finally, a main `run_lock' function to autodetect which type of flock to use, and run the given command with the given lock file.<\/p>\n<blockquote>\n<pre>function run_lock {\n  type -p flock\n  if [ $? -eq 0 ]; then\n    run_lock_flock \"$@\"\n  else\n    run_lock_perl \"$@\"\n  fi\n}\n\nrun_lock mylock.lck \/usr\/bin\/foo -c bar -l baz\n<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>util-linux(-ng) flock On Linux systems, there is a `flock&rsquo; command (from `util-linux&rsquo; or `util-linux-ng&rsquo;) that can be used to run a command inside a file lock\/unlock block. function run_lock_flock { LOCK=$1 shift touch \u00ab\u00a0$LOCK\u00a0\u00bb flock \u00ab\u00a0$LOCK\u00a0\u00bb \u00ab\u00a0$@\u00a0\u00bb } portable way &hellip; <a href=\"https:\/\/locallost.net\/?p=133\">Continuer la lecture <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2Bei9-29","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=133"}],"version-history":[{"count":1,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":172,"href":"https:\/\/locallost.net\/index.php?rest_route=\/wp\/v2\/posts\/133\/revisions\/172"}],"wp:attachment":[{"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/locallost.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}