... 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.

link|improve this question
Are you just looking for something like HOSTNAME=hostname – Zoredache Nov 13 '10 at 1:03
feedback

1 Answer

up vote 2 down vote accepted

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

link|improve this answer
perfect... knew there was an "easy" answer. u prob just saved me hours of head versus brick wall action. thank you! – codemonkey Nov 13 '10 at 1:23
No problem, but be careful - if you pass an argument to that it will change the hostname (at least until the next reboot) – James Lawrie Nov 13 '10 at 1:25
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.