Apache Logrotate Bash 问题

Apache Logrotate Bash 问题

... 所以我尝试每小时轮换一次亚马逊云自动扩展服务器实例上的日志。我创建了 /etc/cron.hourly/logrotate 来读取:

#!/bin/bash

test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate -f /etc/logrotate.conf

我已经将 /etc/logrotate.d/apache2 修改为:

/var/log/apache2/*.log {
    missingok
    rotate 100
    create 640 root adm
    sharedscripts
    postrotate
        neoBucket="widget-chapp/dev/log/";
        neoService="apache";
        neoDate=$(date +\%Y\%m\%d\%H);
        echo "hostname: $HOSTNAME";
        neoHost=`echo "$HOSTNAME" | sed "s/-//g"`;

        # prepend neoService and append YYYYMMDDHH
        for f in *.log.1;
        do
            mv ./$f "$neoHost-$neoService-${f%1}$neoDate";
        done

        # gracefully restart the apache service
        apachectl graceful

        # tar the files
        tar -czf "$neoHost-$neoService-$neoDate.tgz" "$neoHost-$neoService-*.$neoDate"

        echo "neoHost: $neoHost";
        # send the rotated files to s3 bucket
        s3cmd put "$neoHost-$neoService-$neoDate.tgz" s3://$neoBucket > /dev/null

        # remove the individual log files
        rm "$neoHost-$neoService-*.$neoDate";
    endscript
}

...问题是...我如何获取 $HOSTNAME 值...从 postrotate 块中第 10 行的输出来判断它是空的。

答案1

猜测它不在该用户的路径中。尝试/bin/hostname

相关内容