Execute the system command and write the shell script that writes the output to the specified log file (2)
The previous article was a simple script that can be executed and written into logs. However, if it is placed in the production environment, it seems too rough, so further optimization is required:
#! /Bin/basffe [-d "/opt/bmc"]; then if [-f "/opt/bmc/usysfault. log "]; then {date +" % Y-% m-% d % H: % M: % S "/usr/lpp/diagnostics/bin/usysfault # absolute path of the command to be executed}>/opt/bmc/usysfault. log # the absolute path of the log file to be stored, 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 fifiNote:
[-D "xxxx"] determine whether the directory path exists [-f "xxxx"] determine whether the file exists the touch command is used to create an empty file, and the chmod command is used to grant permissions.
Note:
The shell script imposes strict requirements on spaces. You must pay attention to spaces. Otherwise, it is difficult to query the shell script when an error is reported. If conditions must end with fi. Otherwise, an error is reported, which is difficult to query.
Finally, this is the script for executing words. If you want to execute it cyclically, add the while true written in the previous article. However, I use the unix-like scheduled task manager crontab to add scheduled tasks, allowing the system to execute scripts based on the specified scheduled time.