Operating System - HP-UX
1753389 Members
7237 Online
108792 Solutions
New Discussion юеВ

How can I send to logfile based on 'date + %a' in cron?

 
SOLVED
Go to solution
June Franduto
Occasional Contributor

How can I send to logfile based on 'date + %a' in cron?

We have a nightly script that runs in cron that we would like to send to a log file based on the day of the week - i.e. test.log.Mon, test.log.Tue, etc.

When I test at the command line -
/interface/test.sh > /interface/test.log.$(date +%a)

I end up with a log file called "/interface/test.log.Mon"

When I try to put this in cron, the log file gets created as "/interface.test.log." - it ignores the date portion. How can I get this to work the way I need it to?

10 REPLIES 10
A. Clay Stephenson
Acclaimed Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

The problem is that the 'date' command is not in the cron'ed PATH and thus the result from the date command is a null string. Cron has an intentionally very sparse environment. You need to set and export any needed environment variables explicitly within your script or use fully specified pathnames.
If it ain't broke, I can fix that.
Rick Garland
Honored Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

Instead of having cron figure out what the date is, have your script do the data calculation and redirection to the log file.

As mentioned, cron is a sparse environment.

Mel Burslan
Honored Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

if you put the date command's path inside the paranthesis, you should be fine, i.e.,

/interface/test.sh > /interface/test.log.$(/sbin/date +%a)

________________________________
UNIX because I majored in cryptology...
TwoProc
Honored Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

Just putting in the full path to the "date" command should fix it.

try changing :
/interface/test.log.$(date +%a)
to:
/interface/test.log.$(/usr/bin/date +%a)

We are the people our parents warned us about --Jimmy Buffett
June Franduto
Occasional Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

I added the explicit pathname in my crontab, I still have the same results.

/interface/test.sh > /interface/test.log.$(/usr/bin/date +%a)

The script is a generic ftp script that we want to use for several different systems, and we are trying to avoid making system specific changes to the script - we are passing parameters as needed from within cron on each system. We currently send to a specific log file, and I was just trying to make it so that I would have a weeks worth of logfiles to go back to in the event of problems, and also wouldn't have to worry about cleaning them up since they would be overwritten weekly.
TwoProc
Honored Contributor
Solution

Re: How can I send to logfile based on 'date + %a' in cron?

June,
instead of /usr/bin/date or /sbin/date, just try putting in whatever the result of the command "which date" is. As far as avoiding hard-coding it for each machine type - well, just decide on a place for it to run from, and make symbolic links for the other machines. I've had to do this to handle differences between OSs before.

For a plan B - when I've had trouble with cron in the past (similar to what you're experiencing) sometimes I've had to resort to creating a script that has a "#!/bin/ksh" line at the top (to invoke a full ksh), and have the cron job run that script, instead of trying to run the whole command from within cron itself. Put your command with the full path in that new file, or set your environment variables to pick it up from either machine in the PATH variable.

HTH
We are the people our parents warned us about --Jimmy Buffett
June Franduto
Occasional Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

The result of "which date" was /usr/bin/date, since that hadn't worked I opted for Plan B - created another script to run the first script from.

Thanks....
Pradeep_29
Frequent Advisor

Re: How can I send to logfile based on 'date + %a' in cron?

I guess, this is not the path issue. Issue would be with the shell being used.

Try replacing command with test.log.`date +%a`

To know the error in cron, i would suggest to log the error also. Add 2>> at the end. It would help to debug.

~Pradeep.

Fred Martin_1
Valued Contributor

Re: How can I send to logfile based on 'date + %a' in cron?

I let my scripts local write to fixed-named log files, not based on a date.

Then, each night at midnight, a single script runs that copies each log file to an /arch/logs directory, renaming the copy based on date, then purging the orginal.

It also does the same for other system log files (mail, syslog, etc) to keep them small.

Then finally, it deletes older log files from arch (I keep mine for months).

Advantage is, out on the system I don't have various collections of log files. I can easily locate the day's logs because the names are static, or go to /arch/logs to find the older stuff.
fmartin@applicatorssales.com