cron a script and output to file named with date stamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cron a script and output to file named with date stamp
# 1  
Old 04-29-2011
cron a script and output to file named with date stamp

I can do this from the command line:
Code:
/home/mylogin/tests/script.sh > /home/mylogin/tests/`date +"%Y%m%d"`log.csv

It yields a file named: 20110429log.csv

I would like to schedule with cron to run daily.

when I enter the same line, as above in cron:
Code:
10 16 * * * /home/mylogin/tests/script.sh > /home/mylogin/tests/`date +"%Y%m%d"`log.csv

...it trucates at the first percent sign and yields a cron error:
unexpected EOF while looking for matching ``'
...cron executes:
/home/mylogin/tests/script.sh > /home/mylogin/tests/`date +"

It was suggested that I escape the percent sign:
Code:
10 16 * * * /home/mylogin/tests/script.sh > /home/mylogin/tests/`date +"\%Y\%m\%d"`log.csv

However, that doesn't seem to execute at all. I get no ouitput file and no sign of a cron error.

Any insight would be greatly appreciated.

Thanks,
-dog

Last edited by Yogesh Sawant; 05-01-2011 at 07:10 AM.. Reason: added code tags
# 2  
Old 04-29-2011
Backticks mean you want your shell execute the command, but clearly shell is not there to do this when crontab job is executed. I would leave detailed explanation to someone more competent; but I can offer a workaround: Stick the redirection into your script:
Code:
 $ cat /home/mylogin/tests/script.sh
#!/bin/bash
(
 #whatever your script is doing
) > /home/mylogin/tests/`date +%Y%m%d`log.csv

This will run the body of the script in subshell and capture the output into the file
This User Gave Thanks to mirni For This Post:
# 3  
Old 04-29-2011
When I use cron I've found that it is almost always necessary to fully qualify executables. So you might try putting the path in front of the date command.

Also, you might try using single quotes in conjunction with your backticks.

Something along these lines:
Code:
/home/mylogin/tests/script.sh > /home/mylogin/tests/`/bin/date +'%Y%m%d'`log.csv

You may find that just pathing the date command does it for you. (I'm not on my unix box right now and don't recall off the top of my head if date is in /bin)

Good luck!

Last edited by Yogesh Sawant; 05-01-2011 at 07:09 AM.. Reason: added code tags
This User Gave Thanks to topmhat For This Post:
# 4  
Old 05-02-2011
thanks...

I was unable to get the complete output of the script using redirection. I only got the first line.

I was able to get the original cron command line working. I had copied my script to another directory and had neglected to set execute permissions.

Thanks for your help and suggestions!
-dog

Last edited by landog; 05-02-2011 at 10:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding Date Stamp To File Name With sed

trying to add a date stamp to the name of a file with sed. can't seem to comment out the date command so that sed does not only see it as a literal text string this is what I'm trying ls file.txt | sed 's/file.txt/file\/`date`\/.txt/g'but it's giving this file/`date`/.txtlooking for something... (1 Reply)
Discussion started by: jimmyf
1 Replies

2. Shell Programming and Scripting

Shell Script | Parse log file after a given date and time stamp

I am developing one script which will take log file name, output file name, date, hour and minute as an argument and based on these inputs, the script will scan and capture all the error(s) that have been triggered from a given time. Example: script should capture all the error after 13:50 on Jan... (2 Replies)
Discussion started by: ROMA3
2 Replies

3. Shell Programming and Scripting

Perl:Script to append date and time stamp

Help with Perl script : I have a web.xml file with a line <display-name>some_text_here</display-name> Need to append the current date and time stamp to the string and save the XML file Something like <display-name>some_text_here._01_23_2014_03_56_33</display-name> -->Finally want... (5 Replies)
Discussion started by: gaurav99
5 Replies

4. Shell Programming and Scripting

pipe to file named with date

I would like to pipe (redirect ? - what is the right term?) the output of my script to a file named with the current date. If I run this at a command prompt: date +'%Y%m%d" ...it returns "20110429" OK, that's good... so I try: ./script.sh > "'date +%Y%m%d'.csv" I get a file... (1 Reply)
Discussion started by: landog
1 Replies

5. Shell Programming and Scripting

Set date and time stamp of one file to another

Hi I use "touch -t xxxxxxxx" command to set date/time stamp of a file. My requirement is to read the date/time stamp of a file and apply it to another file. Is there anyway to do it simple instead of manually taking date/stamp of first file? TIA Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies

6. Shell Programming and Scripting

append date time stamp via ftp script

I have searched several thread and not found my solution, so I am posting a new qustion. I have a very simple script on an AIX server that FTPs 2 files to a MS FTP server. These 2 files are created on the AIX server every hour, with a static name. I need to FTP the files to the MS server, but... (1 Reply)
Discussion started by: sknisely
1 Replies

7. UNIX for Dummies Questions & Answers

How to Zip the files from date Stamp to end date Stamp

Hi, I need to zip the list of files using from date Stamp to end date Stamp, How can I filter and make FromDate_EndDate.gzip? any idea? (1 Reply)
Discussion started by: redlotus72
1 Replies

8. Shell Programming and Scripting

Date Stamp on new file

Dear Gurus, I'm trying to move a number of files from one directory to another directory with a new date stamp. This is my script: #! /bin/csh Today_Date=`date +%Y%M%D` mv /usr/TRS/data/TS* /usr/TRS/backup/TS*.${Today_Date} when i run the script i'm getting the following errors: mv:... (14 Replies)
Discussion started by: lweegp
14 Replies

9. Shell Programming and Scripting

creating a new file with date stamp

Hi, can any one tell me how to achieve this...I will input the path and file name and it should rename it to current date and time... this is what I tried... #! /usr/bin/sh set -x cd /info_stg/vul/Scripts TODAY_DATE_TIME=`date +%Y%m%d%H%M%S` IN_FILE_PATH=`cat file.txt | awk -F, '{... (2 Replies)
Discussion started by: mgirinath
2 Replies

10. UNIX for Dummies Questions & Answers

File date and time stamp

I have to capture the creation date and time stamp for a file. The ls command doesn't list all the required information. I need year, month, day, hour, minute and second. Any ideas... (1 Reply)
Discussion started by: Xenon
1 Replies
Login or Register to Ask a Question