Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: bakedjake

Message Too Old, No Replies

rotation + date in file name with crontab shellscript

   
5:46 am on Sep 30, 2001 (gmt 0)

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



A friend asked me this:

I need to create a command for crontab containing this:

'cp access.log xxxxxx.log' where xxxxxx represents the output from date
in any format, preferably YYMMDD or YYYYMMDD.

I will set crontab to execute this daily at 23:59:59 saving the day's
access log before it is replaced at 00:00:00

Think you could steer me the right way here?

Now, I could do this with a perl/shell combo, but how would one write a script to do this in straight shell?

8:45 am on Oct 2, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The 'date' command has pretty comprehensive formatting options - for the YYYYMMDD format you'd use

date +%Y%m%d

To use it in a shell script to set a variable, enclose it in backticks ( ` ) - ie:

#!/bin/bash
DATE=`date +%Y%m%d`
cp access.log $DATE.log

10:25 pm on Oct 3, 2001 (gmt 0)

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



SK, I knew you'd pull threw. :)
3:12 pm on Oct 17, 2001 (gmt 0)

10+ Year Member



This is not an answer to the question, but it's related. I keep this line at the top of my crontab files to remind me of the syntax:

# min (0-59), hr (0-23), day of mo (1-31), mo of yr (1-12), day of wk (0-6 with 0=Sunday), command

5:35 am on Oct 22, 2001 (gmt 0)

WebmasterWorld Senior Member littleman is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Nice little tip.