httpd-log in CERN server: newbie question

Albert Lunde Albert-Lunde at nwu.edu
Mon Dec 16 23:12:37 EST 1996


> We are running the CERN server on the City's new proxy server. The httpd-log
> is getting very large. Following the directions in Lincoln Stein's book, I
> sent a
> command 'kill -HUP ###' where ### was the httpd-pid.
>
> The httpd-errors log says it has successfully restarted, and everything is
> working. But it did not start a new httpd-log as I expected it to: it's
> still adding to the old one. What have I missed?

I have no experience with CERN httpd, but I'll offer a guess based on
experience with other software.

A good approach is as follows:

1) rename the log file to a different name *in the same directory*.

2) Then use touch, chmod, and chown to recreate the old log file name with
the desired permissions.

3) Then send a kill -HUP to the server

4) If you are really paranoid and/or your server takes time to stop and
start subprocesses, sleep a bit

5) Move the new file to some other directory, gzip it, or whatever you want.

A unix file/inode can have mutiple references. A program with an open file
keeps the connection with that file, even if it is renamed, so long as the
inode is the same. (Moving a file to a different filesystem could change
the inode.) Sending a kill -HUP often is designed to have the effect of
closing all open files and redeading the configuration files.

Here's something similar I wrote for a cron job to rotate syslog logs, that
_seems_ to work.

It uses a perl script to stash away the result with a timestamp, which is
needlessly complicated, but useful for various things.

= =
# more rotate-syslog
#!/usr/bin/sh -vx
# rotate syslog (under hp/ux 10.20 with some local config changes)

step1() {
 mv $1 $1.old
 touch $1
 chown root:staff $1
 chmod 750 $1
 chown root:staff $1.old
 chmod 750 $1.old

}

step2() {
 # save in /logs/old/syslog/filename/ so we can expire individually
 # create dir if it doesn't already exist
 if [ ! -d /logs/old/syslog/$1 ]; then
    mkdir /logs/old/syslog/$1
 fi
 chown root:staff /logs/old/syslog/$1
 chmod 750 /logs/old/syslog/$1
 # timestamp, move and gzip
 /local-adm/bin/save_log_and_timestamp -z -s $1.old /logs/old/syslog/$1

}

# add one call of step1 and step2 for every file in /etc/syslog.conf
umask 027
PATH="/bin:/usr/bin"
export PATH

cd /var/adm/syslog

step1 "local0.log"
step1 "access.log"
step1 "mail.log"
step1 "syslog.log"

echo  kill -HUP `cat /var/run/syslog.pid`
kill -HUP `cat /var/run/syslog.pid`
sleep 2
/usr/bin/logger syslog rotated


step2 "local0.log"
step2 "access.log"
step2 "mail.log"
step2 "syslog.log"

#
= = = = = =

save_log_and_timestamp source:
= = = = = =
#!/opt/local/bin/perl
# save_log_and_timestamp
# Version 1.0 Albert Lunde 11/19/96 (dev version 5)
# Version 1.1 added -f to gzip

# Options:
#   -h   new name based on time to nearest hour
#   -m   new name based on time to nearest min
#   -s   new name based on time to nearest sec
#   -l   new name based on (l)ast modified time (else current time)
#   -z   gzip the new file
#   -c   copy the file (else move)
#   -v   verbose

require "flush.pl";
use Getopt::Long;


$cp_location = "/usr/bin/cp";
$mv_location = "/usr/bin/mv";
$gzip_location = "/usr/contrib/bin/gzip";

# SUBROUTINES ########################################################


# MAIN BODY ##########################################################
$opt_h="";$opt_m="",$opt_s="",$opt_l="";$opt_z="";$opt_c="";

$ok=GetOptions("h", "m", "s", "l", "z", "m", "c","v");
# print "h=$opt_h m=$opt_m s=$opt_s l=$opt_l z=$opt_z c=$opt_c\n";

$log_file = $ARGV[0];
$archive_dir = $ARGV[1];
$verbose = ($opt_v eq "1");

if((!$ok)||($log_file eq "")||($archive_dir eq "")){
  print STDERR "Save log files with a time stamp name\n";
  print STDERR "Usage:\n\t$0 [options] source-file dest-directory\n";
  print STDERR "\t-h\tnew name based on time to nearest hour\n";
  print STDERR "\t-m\tnew name based on time to nearest min \n";
  print STDERR "\t-s\tnew name based on time to nearest sec \n";
  print STDERR "\t-l\tnew name based on (l)ast modified time (else current time)
 \n";
  print STDERR "\t-z\tgzip the new file\n";
  print STDERR "\t-c\tcopy the file (else move)\n";
  print STDERR "\n";
  exit 1;
}

$archive_dir =~ s/\/$//; # remove trailing slash

if (! $log_file)
{
  print STDERR "Need to specify which file you wish to copy.\n";
  exit 1;
}
elsif (! -e $log_file)
{
  print STDERR "Error: file $log_file does not exist; can't back it up.\n";
  exit 1;
}


if (! $archive_dir)
{
  print STDERR "Need to specify which directory to copy to.\n";
  exit 1;
}
elsif (! -e $archive_dir)
{
  print STDERR "Error: directory $archive_dir does not exist; can't back up to t
hat location.\n";
  exit 1;
}

if (($opt_h && $opt_m)||($opt_h && $opt_s)||($opt_m && $opt_s))
{
  print STDERR "Specify only one of H, M, or S.\n";
  exit 1;
}



if ($opt_l)
{
# last modified
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$b
locks) = stat($log_file);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($mtime);
  #print STDERR "$hour:$min:$sec\n";
}else{
# now
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#print STDERR "$hour:$min:$sec\n";
}


# change posix struct tm into what we really want
$year=sprintf "%04d",($year+1900);
$mon=sprintf "%02d",($mon+1);
$day=sprintf "%02d",$mday;
$hh=sprintf "%02d",$hour;
$mm=sprintf "%02d",$min;
$ss=sprintf "%02d",$sec;



# Handle Options h, m, and s ###########
if (!($opt_h||$opt_m||$opt_s))
{
  $opt_s = 1;  #set seconds as the default resolution
}

# print "h=$opt_h m=$opt_m s=$opt_s l=$opt_l z=$opt_z c=$opt_c\n";

if ($opt_s)
{
  $backup_fname ="$log_file.$year.$mon$day.$hh$mm$ss"
}
elsif ($opt_h)
{
  $backup_fname ="$log_file.$year.$mon$day.$hh"
}
else
{
  $backup_fname ="$log_file.$year.$mon$day.$hh$mm"
}

$new_fname="$archive_dir/$backup_fname";
flush(STDOUT);flush(STDERR);
if(!$opt_c){
   print STDERR "$mv_location $log_file $new_fname\n" if $verbose;
   system "$mv_location $log_file $new_fname";
}else{
   print STDERR "$cp_location -p $log_file $new_fname\n" if $verbose;
   system "$cp_location -p $log_file $new_fname";
};
if ($?)
{
  print STDERR "Error occured while moving/copying.\n";
}


# Now optionally gzip the file. ##########
if ($opt_z)
{
  print STDERR "$gzip_location -f -9 $new_fname\n" if $verbose;
  system "$gzip_location -f -9 $new_fname";
  if ($?)
  {
    print STDERR "Error occured while gzipping.\n";
  }
  $new_fname=$new_fname.".gz";
}
flush(STDOUT);flush(STDERR);

print "$new_fname\n";

exit 0;

# END ###########################################
#
= =

---
    Albert Lunde                      Albert-Lunde at nwu.edu




More information about the Web4lib mailing list