{"id":568,"date":"2013-04-01T12:22:50","date_gmt":"2013-04-01T11:22:50","guid":{"rendered":"http:\/\/blog.stratus.org.uk\/?p=568"},"modified":"2013-04-01T12:33:58","modified_gmt":"2013-04-01T11:33:58","slug":"unix-shell-script-checking-bstgmt","status":"publish","type":"post","link":"https:\/\/blog.stratus.org.uk\/?p=568","title":{"rendered":"Unix Shell Script Checking BST\/GMT"},"content":{"rendered":"<p>A problem over the last weekend when the clocks went forward, showed there was a need\u00a0to do resolve to how the GMT cron jobs should work on a few servers working together (when timing is important).<\/p>\n<p>For a couple of years I simply changed the hour in the <code>crontab <\/code>on each server and left it at that, but alas this is open to error across multiple severs, so I decided to do something about it.<\/p>\n<p>The first step was to put the cron table into GMT hours (i.e. ensure all jobs would run as GMT as normal). The next step was to introduce for the &#8220;morning run&#8221; of work, a check to sleep for an hour if the server is in British Summer Time (BST). We can find this out by using:<\/p>\n<pre>date<\/pre>\n<p>Which can give an output like this:<\/p>\n<pre>Mon Apr  1 12:17:55 BST 2013<\/pre>\n<p>All we want is the local time zone daylight savings flag and then do something if that is BST. We can get just this flag if we use the %Z parameter to the <code>date<\/code> command.<\/p>\n<p>Here&#8217;s a little bit of code to wait if we&#8217;ve run in GMT but need to wait an hour due to BST:<\/p>\n<pre># --- GMT checking - we assume run in GMT timeframe\r\nSLEEPPERIOD=3600\r\nGMTBST=`date '+%Z'`\r\nif [ $GMTBST == BST ]\r\nthen\r\n        # must be in summertime, so wait an hour\r\n        echo \"Found the date has $GMTBST. Sleeping for $SLEEPPERIOD seconds ...\"\r\n        sleep $SLEEPPERIOD\r\nelse\r\n        echo \"Not BST, carrying on\"\r\nfi<\/pre>\n<p>This is then done for the afternoon run and the same again for the evening run.<\/p>\n<p>Note this may cause a problem if multiple scripts check this as they may get serial delays. This works best if you have one main job, and the check is done once.<\/p>\n<p>So now the script will check if it is GMT or BST and wait an hour if BST. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A problem over the last weekend when the clocks went forward, showed there was a need\u00a0to do resolve to how the GMT cron jobs should work on a few servers working together (when timing is important). For a couple of &hellip; <a href=\"https:\/\/blog.stratus.org.uk\/?p=568\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-568","post","type-post","status-publish","format-standard","hentry","category-misc"],"_links":{"self":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=568"}],"version-history":[{"count":14,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":582,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=\/wp\/v2\/posts\/568\/revisions\/582"}],"wp:attachment":[{"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.stratus.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}