关闭

shell脚本打印日志方法

964人阅读 评论(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
查看评论

在 SHELL 脚本里打日志

今天小弟在重构代码中的一个脚本模块,其中涉及到日志功能。上午花了点儿时间想出了个在 shell 打日志的技巧,觉得值得写一下。 希望要实现的效果是:实现一个 write_log 命令,给一条出错消息作为输入,write_log 记录日志时自动加上 时间戳、脚本文件名和行号。形如: 2010-...
  • aabbcc456aa
  • aabbcc456aa
  • 2013-03-06 16:09
  • 411

shell脚本打印日志方法

作者:【吴业亮】云计算开发工程师 博客:http://blog.csdn.net/wylfengyujiancheng在shell脚本执行过程中如果没有日志记录,特别是脚本比较长的情况下在执行完再定位问题很难发现问题原因,因此在脚本中增加日志显得十分重要。下面介绍日志打印方法example 1 ...
  • wylfengyujiancheng
  • wylfengyujiancheng
  • 2015-11-24 22:02
  • 13409

输出执行操作和打印日志的shell脚本

cat /mnt/log_function.sh #!/bin/bash #log function####log_correct函数打印正确的输出到日志文件 function log_correct () { DATE=`date “+%Y-%m-%d %H:%M:%S”` ####显示打...
  • u014241289
  • u014241289
  • 2017-01-18 16:06
  • 1637

自定义shell脚本采集日志信息

一、模拟日志的动态产生在真实的环境中,日志是有nginx或者是tomcat等容器生成的,只需要采集的脚本或者是框架如flume、logstack等。 在本次测试中,采取log4j动态生成来模拟真实环境。 GenerateLog.javapackage log; import org.apache...
  • tianjun2012
  • tianjun2012
  • 2017-03-16 15:41
  • 980

Linux shell脚本输出日志笔记整理

Linux shell脚本输出日志比较整理
  • JenMinZhang
  • JenMinZhang
  • 2016-04-26 17:50
  • 14200

shell脚本打印日志方法

作者:【吴业亮】云计算开发工程师 博客:http://blog.csdn.net/wylfengyujiancheng 在shell脚本执行过程中如果没有日志记录,特别是脚本比较长的情况下在执行完再定位问题很难发现问题原因,因此在脚本中增加日志显得十分重要。如何在日志中记录是哪个用户什么时间执行的...
  • gong_xucheng
  • gong_xucheng
  • 2016-03-22 10:12
  • 1234

监控最新的日志文件的shell脚本

在调试大的项目脚本的时候,有时候每次运行都会产生一个新的日志文件,然后还经常出错,就要每次去找相应的新的日志文件来看看那里出问题了,非常麻烦。。。 所以这个时候脚本就会很有用了,下面的脚本就是用来监控log目录中最后被修改的文件。 [plain] v...
  • sunshine_silence
  • sunshine_silence
  • 2017-03-28 10:13
  • 560

shell脚本自定义日志

  • 2015-12-21 16:52
  • 245B
  • 下载

Linux中的log介绍(三)---内核日志

内核日志的存储  1.    地址 内核的日志存在log_buf指向的内存缓冲区中。 大小在新的内核是可配置的,CONFIG_LOG_BUF_SHIFT=xx  2.  格式  xxxxxx API (1...
  • henni386
  • henni386
  • 2012-04-10 21:51
  • 6088

常用的几个shell脚本_2_查看日志

#! /bin/bash//查看tomcat的后台输出,观察错误所在。tail -f /app/java_project/tomcat5.0/logs/catalina.out 
  • furthersun
  • furthersun
  • 2007-11-30 18:16
  • 541
    个人资料
    • 访问:612821次
    • 积分:9062
    • 等级:
    • 排名:第2473名
    • 原创:319篇
    • 转载:125篇
    • 译文:0篇
    • 评论:89条
    最新评论