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:
- [-D "XXXX"] determine if the directory path exists
- [-F "xxxx"] determine if the file exists
- The touch command is used to create an empty file, and the chmod command is used to assign weights.
Special attention:
- Shell script to the space requirements of the strict, must pay attention to the space, otherwise error you check are difficult to check.
- 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)