Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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 {
    rotate 100
    create 640 root adm
        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;
            mv ./$f "$neoHost-$neoService-${f%1}$neoDate";

        # 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";

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

share|improve this question
Are you just looking for something like HOSTNAME=hostname – Zoredache Nov 13 '10 at 1:03
up vote 2 down vote accepted

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

share|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 L Nov 13 '10 at 1:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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