Linux – Apache Logrotate Bash Question

bashlinuxlogrotateUbuntu

… so I'm trying to rotate logs on amazon cloud auto-scaled server instances every hour. I've created /etc/cron.hourly/logrotate to read:

#!/bin/bash

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

And I've altered /etc/logrotate.d/apache2 to read:

/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
}

… here's the question… how do I get the $HOSTNAME value… judging from output on line 10 in the postrotate block it's empty.

Best Answer

Guessing it's not in the path for that user. Try /bin/hostname