如何在脚本中获取操作日志?

时间:2015-01-29 23:45:29

标签: bash

我是新手,所以请你好好。

我有一个我没写过的脚本(不是提前,也许是一天),我需要它来输出var / logs中的日志。

有人可以帮忙吗?

我需要知道所有操作都在该脚本中完成。

这是脚本:

#!/bin/bash
# copy the old database to a timestamped copy
TIMESTAMP=$(date +%d)
REPORT_EMAIL=user@domain.com

# backup Spamassassin bayes db
sa-learn -p /usr/mailcleaner/etc/mailscanner/spam.assassin.prefs.conf --siteconfigpath /usr/mailcleaner/share/spamassassin --backup >/var/mailcleaner/spool/spamassassin/spamass_rules.bak
# backup Bogofilter bayes db
cp -a /root/.bogofilter/wordlist.db "/root/.bogofilter/$TIMESTAMP-wordlist.db"
if [ -f "/root/.bogofilter/$TIMESTAMP-wordlist.db.gz" ]
then
   rm -f "/root/.bogofilter/$TIMESTAMP-wordlist.db.gz"
fi
gzip "/root/.bogofilter/$TIMESTAMP-wordlist.db"

# get the spam and ham from the imap mailbox for the spamassassin and bogofilter db's
/opt/mailcleaner/scripts/imap-sa-learn.pl
if [ $? -ne 0 ]
then
    (
      echo "Subject: Bogofilter database update $(hostname) failed"
      ls -l /var/mailcleaner/spool/bogofilter/database/
      ) | /usr/sbin/sendmail $REPORT_EMAIL
    exit 1
fi

# copy the database to the right location
cp /root/.bogofilter/wordlist.db /var/mailcleaner/spool/bogofilter/database/wordlist.db
# If slave(s) Mailcleaner exists, ssh copy dbs to the slave(s)
#scp /root/.bogofilter/wordlist.db mailcleaner2.domain.com:/var/mailcleaner/spool/bogofilter/database/
#scp /var/mailcleaner/spool/spamassassin/spamass_rules.bak mailcleaner2.domain.com:/var/mailcleaner/spool/spamassassin/

# get the spam and ham counts from bogofilter - this just prints how many spam and ham you collected so far...
/opt/bogofilter/bin/bogoutil -w /var/mailcleaner/spool/bogofilter/database/wordlist.db .MSG_COUNT

再次感谢您的帮助。

拉​​吉

1 个答案:

答案 0 :(得分:0)

运行脚本时最简单的方法是使用输出重定向:

./the_script.sh > /var/log/the_script.log 2>&1

2>&1用于捕获stderr(错误流)以及stdout(输出流)。

如果要编写日志文件,请在控制台上查看输出,请使用tee

./the_script.sh 2>&1 | tee /var/log/the_script.log