{"id":2323,"date":"2015-12-03T09:27:11","date_gmt":"2015-12-03T17:27:11","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2323"},"modified":"2016-03-10T17:25:07","modified_gmt":"2016-03-11T01:25:07","slug":"log-results-of-perl-scripts","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2323","title":{"rendered":"Log results of perl scripts"},"content":{"rendered":"<p>While you are debugging perl scripts, the default for print statements is to display on the console. Often you want to have more info than fits nicely on the console or you want to be able to search through the results. In that case you can redirect the print commands with a simple redirect. e.g.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n.\/perl_test.pl &gt; test_results.txt\n<\/code><\/pre><\/p>\n<p>Once the script is debugged and running as a cron job, you might still want to see that it has successfully completed or has generated errors. In that case, redirecting the STDOUT and STDERR to log files is what you want to do.<\/p>\n<p>I created a directory \/var\/log\/My_logs and chmodded it to 755 and chowned it to admin. Then I added two lines at the beginning of the file.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n#! \/usr\/bin\/perl\n\nuse strict;\nuse warnings;\n\nopen(STDOUT, &#039;&gt;&gt;&#039;,&nbsp;&nbsp;$0 . &quot;.log&quot;) or die &quot;Can&#039;t open log&quot;;\nopen(STDERR, &#039;&gt;&gt;&#039;,&nbsp;&nbsp;$0 . &quot;.error.log&quot;) or die &quot;Can&#039;t open error log&quot;;\n<\/code><\/pre><\/p>\n<p>A couple of notes. the >> appends the results to the current file. $0 is the name of the perl script that is running. So if I run perl_test.pl, the log file is perl_test.l.log. Also note that that is the complete file path and file name. If you are running the script manually, you might want to create log files in the same directory as the script. But if the script is part of a cron job, you might be better off writing the log files to \/var\/log\/. And you probably don\u2019t want the entire script do die if the log file can\u2019t be opened. <\/p>\n<p>An alternative way to do the same thing is to split the file name into parts with the fileparse function in the  File::Basename module. <\/p>\n<p><pre><code class=\"preserve-code-formatting\">\n#! \/usr\/bin\/perl\n\nuse strict;\nuse warnings;\n\n my($filename, $directories, $suffix) = fileparse($path);\n\nopen(STDOUT, &#039;&gt;&gt;&#039;, &quot;\/var\/log\/My_Project_logs\/&quot; . $filename. &quot;.log&quot;);\nopen(STDERR, &#039;&gt;&gt;&#039;, &quot;\/var\/log\/My_Project_logs\/&quot; . $filename. &quot;.error.log&quot;);\n<\/code><\/pre><\/p>\n<p>If you run the script as you, but later want it to be run as a cron job, make sure you change the permissions of the log files so that they match the permissions of the owner of the script.<\/p>\n<p>If you write out a bunch of stuff, make sure you clean out the log files from time to time. Otherwise, set up a log rotation in \/etc\/logrotate.d\/. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>While you are debugging perl scripts, the default for print statements is to display on the console. Often you want to have more info than fits nicely on the console or you want to be able to search through the results. In that case you can redirect the print commands with a simple redirect. e.g. &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2323\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Log results of perl scripts<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-2323","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2323"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}