我有一个计划的unix脚本,我想记录输出。由于用户界面的限制,我无法编辑cron文件,我无法添加>> logfile到命令。我可以在脚本本身添加一些内容来将输出发送到日志吗?
{
printf poo
#Do not change
PRINTF=/usr/bin/printf
MSMTP=/usr/local/bin/msmtp
MSMTPCONF=/var/etc/msmtp.conf
#Can be changed
FROM="nas4free@usinfosec.com"
TO="dpatino@usinfosec.com"
MDIR="CaseData"
SUBJECT="$MDIR Backup Report"
} > /mnt/support/logs/$SUBJECT.log
#BODY="$(cat /mnt/support/logs/test.log)"
#$PRINTF "From:$FROM\nTo:$TO\nSubject:$SUBJECT\n\n$BODY" | $MSMTP --file=$MSMTPCONF -t
答案 0 :(得分:6)
尝试
#!/bin/bash
exec > /tmp/myLog.log 2>&1
set -x
日志显示:
+ echo 'Hello World!'
Hello World!
答案 1 :(得分:2)
一种方法是将脚本包装在大括号中并重定向输出,如下所示:
#!/bin/bash
{
# script contents here
echo running script
} > logfile
答案 2 :(得分:0)
在开始编写脚本时附加以下行
log_file_path="/tmp/output.log"
log() { while IFS='' read -r line; do echo "$line" >> "$log_file_path"; done; };
exec > >(tee >(log)) 2>&1
您的脚本经过修改
PRINTF=/usr/bin/printf
MSMTP=/usr/local/bin/msmtp
MSMTPCONF=/var/etc/msmtp.conf
FROM="nas4free@usinfosec.com"
TO="dpatino@usinfosec.com"
MDIR="CaseData"
SUBJECT="$MDIR Backup Report"
{
printf poo
} > /mnt/support/logs/$SUBJECT.log
#BODY="$(cat /mnt/support/logs/test.log)"
#$PRINTF "From:$FROM\nTo:$TO\nSubject:$SUBJECT\n\n$BODY" | $MSMTP --file=$MSMTPCONF -t