• 运行系统命令而且将输出写到指定日志文件的shell脚本(2)


    上一篇是个简单的能够运行而且写入日志的脚本,可是假设放到生产环境上就显得太粗糙了,所以须要进一步的优化:

    #! /bin/bash
    
    if [ -d "/opt/bmc" ] ; then
        if [ -f "/opt/bmc/usysfault.log" ] ; then
            {
                date +"%Y-%m-%d %H:%M:%S"
                /usr/lpp/diagnostics/bin/usysfault    #要运行的命令的绝对路径
            } > /opt/bmc/usysfault.log     #要存放日志文件的绝对路径
        else
            touch /opt/bmc/usysfault.log
            chmod 755 /opt/bmc/usysfault.log
            {
                date +"%Y-%m-%d %H:%M:%S"
                /usr/lpp/diagnostics/bin/usysfault
            } > /opt/bmc/usysfault.log
        fi
    else
        if [ -f "/tmp/usysfault.log" ] ; then
            {
                date +"%Y-%m-%d %H:%M:%S"
                /usr/lpp/diagnostics/bin/usysfault
            } > /tmp/usysfault.log
        else
            touch /tmp/usysfault.log
            chmod 755 /tmp/usysfault.log
            {
                date +"%Y-%m-%d %H:%M:%S"
                /usr/lpp/diagnostics/bin/usysfault
            } > /tmp/usysfault.log
        fi
    fi
    凝视: 

    1. [ -d "xxxx" ] 推断文件夹路径是否存在
    2. [ -f  "xxxx" ] 推断文件是否存在
    3. touch 命令用于创建空文件。chmod 命令用于赋权。

    特别注意:

    1. shell脚本对空格的要求异常的严格,必须注意空格,否则报错了你查都非常难查。
    2. if 条件使用结束时必须有fi结尾。不然会报错,也是非常难查了。

    最后,这个是单词运行的脚本,假设要循环运行的话加上上一篇写的while true就可以。只是我採用的是类unix系统的计划任务管理器crontab来加入计划任务,让系统按指定的计划时间来运行脚本。

  • 相关阅读:
    C++内存管理
    GitHub 简单用法
    Tembin
    git
    js 插件使用总结
    cas sso
    Redis实战
    全面分析 Spring 的编程式事务管理及声明式事务管理
    mybatis
    b2b
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6896178.html
Copyright © 2020-2023  润新知