get the timestamp as a log filename

Tom Mitchell mitch48 at sbcglobal.net
Fri Jan 23 17:51:36 UTC 2004


On Fri, Jan 23, 2004 at 06:37:28AM -0500, Dave Rinker wrote:
> Tom,
> 
> This is a script I cron every night at midnight.  crude but it works
> without flaw. modify the "date" line to whatever you want it to be.
> #!/bin/sh
> date=`/bin/date +%b%d%y`
> cp /var/logs/messages /var/logs/messages.$date

As a personal matter of preference, I would reorganize the date string
so "ls" can sort the files in order.  You are using:

  $ /bin/date +%b%d%y
  Jan2304

which does not produce a file name that sorts the way I like.
I would use something like...

  $ date +"%Y-%m-%d_%R"
  2004-01-23_09:01

If I was extra cautious I would capture the time zone in the name 
and perhaps force GMT.
  $ date -u +"%Y-%m-%d_%R"
  2004-01-23_17:03
  $ date -u +"%Y-%m-%d_%R%Z"
  2004-01-23_17:05UTC
  $ date +"%Y-%m-%d_%R%Z"
  2004-01-23_09:09PST

Your date string avoids the confusion of month/day day/month!
	09/12  Dec 9 or Sept 12.

> > 	mv logfile.1 logfile-ending-on-`date +"%Y-%m-%d_%R`

I need to think about how your script interacts with the log daemon
and logrotate.  Processes that log to files often keep the file open
thus the mv is done first.  Then a new file is created if needed.
Then the process is signaled so it can close the old file's inode.
The rename acts on the inode that is open.  If you make a copy of a
file that is open it only contains the data at that instant.  I think
there is a small window of time where some stuff can be lost in your
solution.



-- 
	T o m  M i t c h e l l 
	mitch48-at-sbcglobal-dot-net





More information about the fedora-list mailing list