{"id":134,"date":"2016-01-28T23:25:51","date_gmt":"2016-01-28T23:25:51","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=134"},"modified":"2016-02-08T20:56:06","modified_gmt":"2016-02-08T20:56:06","slug":"command-line-fun-logs-and-logging","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/01\/28\/command-line-fun-logs-and-logging\/","title":{"rendered":"command line fun &#8211; logs and logging"},"content":{"rendered":"<p>Before writing this blog entry, I never considered the actual similarities between the Linux shell and the dos shell. I shouldn&#8217;t have been too surprised as the similarities have less to do with the operating systems than with the general theory.<\/p>\n<p>The shell is all about text. Command line programs accept text as input or generate text as output.\u00a0 When things go well the text goes to standard output, but when things don&#8217;t go well then the messages go to standard error.<\/p>\n<p>Output on the command prompt isn&#8217;t entirely obvious when it is printed.\u00a0 The content may give an indication if it says something like &#8220;syntax error&#8221; but there is a better way to determine the real problems.\u00a0 The output streams are separate and can be redirected either separately or together to a log file.<\/p>\n<p>All the output is displayed together in the terminal window but can be split into its composite parts.\u00a0 In Unix each program inherits three standard streams, which is how the program can then split the output into either standard output or standard error.<\/p>\n<h3>Standard IO Streams<\/h3>\n<table class=\"w3-table-all\" style=\"width: 639px; height: 177px;\">\n<tbody>\n<tr>\n<th>Fileno<\/th>\n<th>Name<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>STDIN<\/td>\n<td>Standard input<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>STDOUT<\/td>\n<td>Standard output<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>STDERR<\/td>\n<td>Standard error<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For example, the directory listing output of the <strong>ls<\/strong> program is sent to standard output but the error is sent to standard error.<\/p>\n<div class=\"sbody-code\">\n<pre><code>sam@asus:\/tmp\/testarea$ ls -ltrR\r\n.: total 388\r\n-rw-r--r-- 1 sam sam 146962 Nov 7 23:54 Steigenberger Airport_Kinder_009.jpg\r\n-rw-r--r-- 1 sam sam 92444 Nov 7 23:54 Steigenberger Airport_Kinder_010.jpg\r\n-rw-r--r-- 1 sam sam 148526 Nov 7 23:54 Steigenberger Airport_Kinder_031.jpg\r\nd--------- 2 sam sam 4096 Nov 7 23:55 private\r\nls: cannot open directory .\/private: Permission denied \r\nsam@asus:\/tmp\/testarea$<\/code><\/pre>\n<\/div>\n<p>The output can be redirected to a file by using the greater than &#8220;&gt;&#8221; symbol.<\/p>\n<div class=\"sbody-code\">\n<pre><code>sam@asus:\/tmp\/testarea$ ls -ltrR &gt; listing.txt\r\nls: cannot open directory .\/private: Permission denied \r\nsam@asus:\/tmp\/testarea$\r\n<\/code><\/pre>\n<\/div>\n<p>Yet, because the error output is not redirected, it is still displayed in the terminal window.<\/p>\n<p>It is possible to only redirect the error messages to the error file.<\/p>\n<div class=\"sbody-code\">\n<pre><code>sam@asus:\/tmp\/testarea$ ls -ltrR 2&gt; error.txt\r\n.: total 388\r\n-rw-r--r-- 1 sam sam 146962 Nov 7 23:54 Steigenberger Airport_Kinder_009.jpg\r\n-rw-r--r-- 1 sam sam 92444 Nov 7 23:54 Steigenberger Airport_Kinder_010.jpg\r\n-rw-r--r-- 1 sam sam 148526 Nov 7 23:54 Steigenberger Airport_Kinder_031.jpg\r\nd--------- 2 sam sam 4096 Nov 7 23:55 private\r\n<\/code><\/pre>\n<\/div>\n<p>Yet, it is easy to combine the output of both streams into a single output file by adding &#8220;2&gt;&amp;1&#8221; to the end of the command.\u00a0 This will then redirect all standard error output into the standard output.<\/p>\n<div class=\"sbody-code\">\n<pre><code>sam@asus:\/tmp\/testarea$ ls -ltrR 2&gt;&amp;1 output.log\r\n.: total 388\r\n-rw-r--r-- 1 sam sam 146962 Nov 7 23:54 Steigenberger Airport_Kinder_009.jpg\r\n-rw-r--r-- 1 sam sam 92444 Nov 7 23:54 Steigenberger Airport_Kinder_010.jpg\r\n-rw-r--r-- 1 sam sam 148526 Nov 7 23:54 Steigenberger Airport_Kinder_031.jpg\r\nd--------- 2 sam sam 4096 Nov 7 23:55 private\r\n<\/code><\/pre>\n<\/div>\n<h3>Special output locations<\/h3>\n<p>It is possible to decide which output is important.\u00a0 In some situations it may be the normal (standard) output while in perhaps for production situations it is only any errors that occur that are interesting.<\/p>\n<p>On both Linux and dos it is possible to easily throw away the unnecessary output.\u00a0 The virtual garbage can is called the null device.\u00a0 So, to eliminate unneeded output simply redirect the output to the null file.<\/p>\n<p>The null file is called \/dev\/nul on linux and NUL on windows.<\/p>\n<p>Ignore standard output example<\/p>\n<div class=\"sbody-code\">\n<pre><code>Linux\r\n   ls -l file.xxx &gt; \/dev\/null\r\n\r\nWindows\r\n   dir file.xxx &gt; nul<\/code><\/pre>\n<\/div>\n<p>Ignore error example<\/p>\n<div class=\"sbody-code\">\n<pre><code>Linux\r\n   ls -l file.xxx 2&gt; \/dev\/null\r\n\r\nWindows\r\n   dir file.xxx 2&gt; nul<\/code><\/pre>\n<\/div>\n<h3>Other output methods &#8211; tee<\/h3>\n<p>One of the really neat things about the Unix world is the wealth of command line programs which simplify daily operations.\u00a0 One of my favorite command line programs is <em><strong>tee<\/strong><\/em>.\u00a0 Simply run some program and pipe the output through the tee command.\u00a0 The output will be both displayed both on the screen but also logged to the file given.<\/p>\n<div style=\"margin-left: 1cm;\">\n<p><code><code>ie.<\/code><\/code>ls -ltr | tee mylogfile.log<\/p>\n<\/div>\n<p>This is ideal when you wish to run some program or script on the command line and see what is being printed but still wish to save the standard output to a log file.\u00a0 But if the error output should also be saved, then simply redirect the standard error output to the standard output stream.<\/p>\n<p>There is no standard program or utility that is similar to the Linux tee command in the windows world but there is a similar utility that can be <a href=\"https:\/\/code.google.com\/p\/wintee\/\">downloaded<\/a> for windows to supplement windows scripts.<\/p>\n<h3>Other output methods &#8211; script<\/h3>\n<p>It is all well and good to redirect output.\u00a0 It is possible to see what the output was or to get a small extract of the errors that occurred, but there is a better way to see not only the run of a single command but everything that occurred at the command line for a certain period of time.<\/p>\n<p>The Linux command &#8220;script&#8221; will save every key pressed and every character or message that was output to a output file.<\/p>\n<p>If the command is run from the command prompt without any parameters then all output will be saved into a file called &#8220;typescript&#8221;.\u00a0 If a filename is given as a parameter then all output will be saved to that file.\u00a0 All key presses or output will be stored into the output file until you terminate the script command.\u00a0 This is done by either typing &#8220;exit&#8221; or with a control-d.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before writing this blog entry, I never considered the actual similarities between the Linux shell and the dos shell. I shouldn&#8217;t have been too surprised as the similarities have less to do with the operating systems than with the general &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/01\/28\/command-line-fun-logs-and-logging\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[18,17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/134"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":18,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":635,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/134\/revisions\/635"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}