Shell script that executes system commands and writes output to the specified log file (2)

Source: Internet
Author: User
Tags touch command

The previous article is a simple script that can be executed and written to the log, but if it is too coarse to put into production, further optimizations are needed:

#! /bin/bashif [-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    fielse    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    Fifi
Comments:

    1. [-D "XXXX"] determine if the directory path exists
    2. [-F "xxxx"] determine if the file exists
    3. The touch command is used to create an empty file, and the chmod command is used to assign weights.

Special attention:

    1. Shell script to the space requirements of the strict, must pay attention to the space, otherwise error you check are difficult to check.
    2. If condition use end must have fi end, otherwise will error, also is difficult to check.

Finally, this is the script for the execution of the word, and if you want to loop execution, add the last write while true. But I'm using the Unix-like system's scheduled Task manager, Crontab, to add a scheduled task that allows the system to execute the script at the specified scheduled time.

Shell script that executes system commands and writes output to the specified log file (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.