我有这个bash脚本,我有一个名为LogMsg的函数,我用它将几个命令的所有输出记录到一个文件中。
但是,当我尝试从另一个bash脚本重定向到stderr和stdout时,只记录第一行,并且其余的脚本似乎没有运行(在我的bash文件中调用脚本)。
#!/bin/bash
#Contains all executions for backup jobs
function LogMsg {
now="$(date +'%d/%m/%Y %T')"
if [ -n "$1" ]
then
IN="$1"
else
read IN
fi
echo "$now [LOG]: $IN" >> $LOG_FILE;
}
LogMsg "Starting backup procedure"
LogMsg "Dumping MongoDB databases"
/bin/bash /root/mongodump.sh 2>&1 | LogMsg
LogMsg "Moving all backups to Backup server"
这将记录以下内容:
18/01/2018 14:28:32 [LOG]: Starting backup procedure
18/01/2018 14:28:14 [LOG]: Dumping MongoDB databases
18/01/2018 14:28:14 [LOG]: connected to: localhost
18/01/2018 14:28:14 [LOG]: Moving all backups to Backup server
但是,如果我删除了2>&1 | LogMsg
,那么执行mongodump.sh的其余部分,并在shell中发出更多信息:
root@Debian-81-jessie-64-LAMP:~# ./backupjobs.sh
connected to: localhost
Thu Jan 18 14:28:41.204 DATABASE: example to /root/backups/MongoDB/2018/01/18/14:28:41/example
Thu Jan 18 14:28:41.205 example.system.indexes to /root/backups/MongoDB/2018/01/18/14:28:41/example/system.indexes.bson
Thu Jan 18 14:28:41.205 16 objects
答案 0 :(得分:0)
LogMsg只有一个读取,然后结束,杀死mongodump.sh。 LogMsg应循环,直到读完所有内容。