{"id":120,"date":"2008-01-11T00:42:34","date_gmt":"2008-01-11T05:42:34","guid":{"rendered":"http:\/\/www.dannorris.com\/2008\/01\/11\/on-handling-logging-in-a-script\/"},"modified":"2019-04-01T13:58:29","modified_gmt":"2019-04-01T13:58:29","slug":"on-handling-logging-in-a-script","status":"publish","type":"post","link":"https:\/\/www.dannorris.com\/blog\/2008\/01\/11\/on-handling-logging-in-a-script\/","title":{"rendered":"On handling logging in a script"},"content":{"rendered":"<p>\t\t\t\tThis tip isn&#8217;t really new, but it is one of those things that I used to know, forgot, remembered, and am now blogging so that it will be easier to find (for me) when I finally forget it again. Besides, I was hanging out with <a href=\"http:\/\/bexhuff.com\/\">Bex Huff<\/a> earlier tonight and he told me I need to blog more and this was what popped into my head. On a side note, I would encourage everyone to seek out someone that you &#8220;know&#8221; from this internet thang when you&#8217;re traveling. Meeting in person certainly beats any &#8220;8 things&#8221; list :). I happen to be in Minneapolis this month, so I looked up Bex and also hope to find a bar or similar establishment where <a href=\"http:\/\/blogs.oracle.com\/fusionecm\/\">Billy<\/a> can don the kilt 2.0 for another picture (that may be asking a lot in 31-degree temps, though).<\/p>\n<p>Enough soapbox&#8230;on with the tech talk. <!--more-->I&#8217;ve had several occasions where I intended to capture all output from commands I put into a shell script, but somehow forgot one or bungled the output redirection of one of them about half way through the script and overwrote the log information (put &#8220;&gt;&#8221; instead of &#8220;&gt;&gt;&#8221;). Anyway, this method resolves that and also captures *all* of the output from the commands in the script. The best part is that you don&#8217;t have to add all those output redirections to each command. It&#8217;s really pretty short syntax, but you have to get it right.<\/p>\n<p>Here&#8217;s what you do:<\/p>\n<p><code>#!\/bin\/bash<br \/>\n### this one was tested in bash, but should work equally well in other sh-like shells.<br \/>\nLOG=\/path\/to\/logfile.txt<br \/>\nrm -f $LOG ### important to make sure someone didn't symlink it to your db file<\/code><br \/>\n<code><br \/>\n### save the file descriptors<br \/>\nexec 3&gt;&amp;1<br \/>\nexec 4&gt;&amp;2<\/code><br \/>\n<code><br \/>\n### set up output streams<br \/>\nexec 1&gt; $LOG<br \/>\nexec 2&gt;&gt; $LOG<br \/>\n<code><br \/>\n### do stuff, run commands, whatever<br \/>\n### all the commands run here will be logged to the $LOG file<br \/>\n<code><br \/>\n### restore the file descriptors<br \/>\nexec 1&gt;&amp;3<br \/>\nexec 2&gt;&amp;4<br \/>\n### commands that come here will not be logged to the $LOG<\/code><\/code><\/code><\/p>\n<p>That&#8217;s it, pretty short and sweet. Hope it helps you&#8211;I know it makes for much prettier scripts and generally makes for more helpful logfiles too. If any of the scripting gurus have suggestions for improvements, please comment below or drop me an email.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tip isn&#8217;t really new, but it is one of those things that I used to know, forgot, remembered, and am now blogging so that it will be easier to find (for me) when I finally forget it again. Besides, I was hanging out with Bex Huff earlier tonight and he told me I need &hellip; <a href=\"https:\/\/www.dannorris.com\/blog\/2008\/01\/11\/on-handling-logging-in-a-script\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;On handling logging in a script&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,22],"tags":[157,178,192,201],"class_list":["post-120","post","type-post","status-publish","format-standard","hentry","category-scripts","category-technical","tag-output","tag-redirection","tag-scripting","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":1,"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/posts\/120\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dannorris.com\/blog\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}