关闭

shell脚本打印日志方法

831人阅读 评论(0) 收藏 举报
分类:


在Shell脚本执行过程中如果没有日志记录,特别是脚本比较长的情况下在执行完再定位问题很难发现问题原因,因此在脚本中增加日志显得十分重要。如何在日志中记录是哪个用户什么时间执行的哪个脚本,执行结果又是什么呢?下面介绍日志打印方法

example 1 
调用log_info函数,如果希望只记录相关信息,不对命令执行结果进行判断 
判断/etc/passwd是否存在,如果存在就记录一条日志“/etc/passwd is exist.”

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-operator" style="box-sizing: border-box;">-f</span>  /etc/passwd  ]
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> 
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-operator" style="box-sizing: border-box;">-e</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\033[32m /etc/passwd is exit \033[0m"</span>
    log_info <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/etc/passwd is exist."</span>    
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

example 2 
调用fn_log函数,如果命令执行成功就回显绿色信息,失败回显红色信息。并在日志中记录成功或失败信息。

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;">rm <span class="hljs-attribute" style="box-sizing: border-box;">-f</span> /tmp/messages <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">&&</span>  cp <span class="hljs-attribute" style="box-sizing: border-box;">-a</span> /<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">var</span>/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">log</span>/messages  /tmp
fn_log <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"rm -rf /tmp/message &&  cp -a /var/log/message  /tmp"</span>

ping <span class="hljs-attribute" style="box-sizing: border-box;">-c</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.10</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.10</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.10</span> 
fn_log <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ping -c 3 10.10.10.10 "</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

这里写图片描述

example 3 
如果脚本在执行过程中用户按了CTR+C 终止脚本执行,回显“DO NOT SEND CTR + C WHEN EXECUTE SCRIPT !!!!”,并在日志中记录。 
这里写图片描述 
记录日志函数代码如下:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-shebang" style="color: rgb(0, 102, 102); box-sizing: border-box;">#!/bin/bash</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#log path is /var/log/openstack-kilo</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#if commod execute sucessed,it will return 0 else return 1</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Copyright 2014 Intel Corporation, All Rights Reserved.</span>
function <span class="hljs-function" style="box-sizing: border-box;">log_info</span> ()
{
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [  <span class="hljs-operator" style="box-sizing: border-box;">-d</span> /var/log  ]
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>
    mkdir -p /var/log 
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span>

DATE_N=`date <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"+%Y-%m-%d %H:%M:%S"</span>`
USER_N=`whoami`
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${DATE_N}</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${USER_N}</span> execute <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$0</span> [INFO] <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span>"</span> >>/var/log/openstack-kilo <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#执行成功日志打印路径</span>

}

function <span class="hljs-function" style="box-sizing: border-box;">log_error</span> ()
{
DATE_N=`date <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"+%Y-%m-%d %H:%M:%S"</span>`
USER_N=`whoami`
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-operator" style="box-sizing: border-box;">-e</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\033[41;37m <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${DATE_N}</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">${USER_N}</span> execute <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$0</span> [ERROR] <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span> \033[0m"</span>  >>/var/log/openstack-kilo <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#执行失败日志打印路径</span>

}

function <span class="hljs-function" style="box-sizing: border-box;">fn_log</span> ()  {
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [  $? <span class="hljs-operator" style="box-sizing: border-box;">-eq</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  ]
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>
    log_info <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span> sucessed."</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-operator" style="box-sizing: border-box;">-e</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\033[32m <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span> sucessed. \033[0m"</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
    log_error <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span> failed."</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">echo</span> <span class="hljs-operator" style="box-sizing: border-box;">-e</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\033[41;37m <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$@</span> failed. \033[0m"</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">exit</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span>
}
trap <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'fn_log "DO NOT SEND CTR + C WHEN EXECUTE SCRIPT !!!! "'</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span></code><code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;"><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;">作者:【吴业亮】</span><a target=_blank href="http://lib.csdn.net/base/20" class="replace_word" title="Hadoop知识库" target="_blank" style="font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px; text-decoration: none; box-sizing: border-box; color: rgb(223, 52, 52); font-weight: bold;">云计算</a><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;">开发工程师 </span><br style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px; box-sizing: border-box;" /><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px;">博客:</span><a target=_blank href="http://blog.csdn.net/wylfengyujiancheng" style="font-family: 'microsoft yahei'; font-size: 14px; line-height: 35px; text-decoration: none; box-sizing: border-box; color: rgb(12, 137, 207);">http://blog.csdn.net/wylfengyujiancheng</a>
</span></code>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:462368次
    • 积分:7587
    • 等级:
    • 排名:第3164名
    • 原创:305篇
    • 转载:124篇
    • 译文:0篇
    • 评论:75条
    最新评论