Date in filename from crontab

Hi,
on AIX
The following

echo "rr" > myfile_$(date +"%m_%d_%Y").log

gives:

myfile_10_23_2019.log

But in crontab:

00 03 * * 1-6 /u01/script.sh >/tmp/myfile_$(date +"%m_%d_%Y").log 2>&1

gives:

myfile_Wed Oct 23 03:00:00 CEST 2019

How can one have:

myfile_10_23_2019.log

when running from crontab?

Thanks.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Hi,

You need to ensure that /u01/script.sh sources your environment.

Regards

Gull04

Could it be the space? Try

00 03 * * 1-6 /u01/script.sh >"/tmp/myfile_$(date +'%m_%d_%Y').log" 2>&1

instead.

If that doesn't work, try putting

exec > "/tmp/myfile_$(date +'%m_%d_%Y').log"
exec 2>&1

at the beginning of your script. Caveat: I can't remember whether the exec 2>&1 should go before or after the other exec statement.

Andrew

Hi Guys,

This request has been posted a number of times, as a simple search will show - thst being said this thread is probably a good place to start.

Regards

Gull04

man crontab yields (among others):

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character,
will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile. Percent-signs (%) in the command,
unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as 
standard input. 
2 Likes

Thanks.
Answer: