How can i make my cron/script to generate a log filename with timestamp in it ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i make my cron/script to generate a log filename with timestamp in it ?
# 1  
Old 01-14-2015
How can i make my cron/script to generate a log filename with timestamp in it ?

Hello Friends,

I would like my script to display date timestamps in the file name for every script execution.

Below is the scenario: (just for testing purpose)
I scheduled a cron job, lets say it runs every 5 min and record/logs output in to a log file.

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/test/mytestscpt.sh > /tmp/test/logs/crnlog.log

Here what i wanted to do; instated of over writing or appending the output to an existing log file "crnlog.log"; i would like to create/have separate response/output log file with time stamp in its name.

I tried to make use of "date +%m_%d_%y-%H.%M.%S " ; but was not able to achieve it.

for example;

first run,
it should create
/tmp/test/logs/crnlog.01_14_15-12.00.45.log

after 5 min,
it should run and create
/tmp/test/logs/crnlog.01_14_15-12.05.45.log


another 5 min after that,
It should create
/tmp/test/logs/crnlog.01_14_15-12.10.45.log

And so on.

Could you please give me some idea to implement this?

Thank you.

Last edited by System Admin 77; 01-14-2015 at 01:21 PM..
# 2  
Old 01-14-2015
Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/test/mytestscpt.sh > /tmp/test/logs/crnlog-$(date +%Y-%m-%d-%H-%M-%S).log

Dates in YYYY-MM-DD-HH-MM-SS order sort, compare, and match easily.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-14-2015
@Corona688
Awesome. Thanks much for the prompt response. I really appreciate.
Initially i tried like below

/tmp/$"date +%m_%d_%y-%H.%M.%S".log

i was just doing trial and error. finally i got, what i was looking for in few seconds. that's great.


Thank you.
# 4  
Old 01-14-2015
If by chance a yearly retention is wanted, you can simply leave out the years and seconds, and it will overwrite the logs after one year:
Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/test/mytestscpt.sh > /tmp/test/logs/crnlog-$(date +\%m-\%d-\%H-\%M).log

Alternatively, a separate cleanup process can be run from the same cron entry:
Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * find /tmp/test/logs -type f -name "crnlog-*.log" -mtime +365 -delete; /tmp/test/mytestscpt.sh > /tmp/test/logs/crnlog-$(date +\%Y-\%m-\%d-\%H-\%M-\%S).log


Last edited by MadeInGermany; 01-14-2015 at 04:27 PM.. Reason: crontab needs \%
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 01-14-2015
Quote:
Originally Posted by System Admin 77
Hi,

Just wanted to confirm something.

Actually when i run below command
/tmp/test/mytestscpt.sh > /tmp/test/logs/crnlog-$(date +%Y-%m-%d-%H-%M-%S).log

its giving output similar to what i expected.
crnlog-2015-01-14-12-49-48.log


But when schedule same on cron its giving output like below

Code:
-rw-r--r--    1 root     system           69 Jan 14 12:45 crnlog-Wed Jan 14 12:45:00 EST 2015
-rw-r--r--    1 root     system           38 Jan 14 12:46 crnlog-Wed Jan 14 12:46:00 EST 2015

Any suggestion pls.
Please don't ask technical questions in private messages.

I just remembered that % is special to cron, and must be escaped like \%.

Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /tmp/test/mytestscpt.sh > /tmp/test/logs/crnlog-$(date +\%m-\%d-\%H-\%M).log

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 01-14-2015
am sorry. I will make a note of it. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort the timestamp in the filename in shell script?

originally the shellscript #ln_file_name=`echo $ld_interface_date"_"${8}".csv"` #ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv` ln_file_name="*"`echo ${7}".csv"` get_file_list_1=$log_path"tm1_file_list.gfl1" cd ${source_path} echo "Try to find any file exist in the... (10 Replies)
Discussion started by: feilhk
10 Replies

2. Shell Programming and Scripting

Filename timestamp

Hi Gurus, I have different files with different timestamp and different base file name, I have to group those files based on basename and provide a unique file name for similar file names. My Directory has following files. abc_filename_20130623:00:09:00.txt... (1 Reply)
Discussion started by: user_linux
1 Replies

3. Shell Programming and Scripting

Get filename with size and timestamp

Hi, Below is a directory containing links new2,list,new1. I need to get the size and timestamp for them. How do i get these details. Please help lrwxrwxrwx 1 xxx abc 11 Nov 24 17:34 new2 -> ./org1/new2 lrwxrwxrwx 1 xxx abc 11 Nov 24 17:34 list -> ./org2/list lrwxrwxrwx 1 xxx abc 10... (2 Replies)
Discussion started by: pradebban
2 Replies

4. Shell Programming and Scripting

How to extract timestamp from the filename?

Hi., My file name is of the format: name_abc_20100531_142528.txt where., my timestamp is of the format: yyyymmdd_hhmmss How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input? I want to get the variables... (9 Replies)
Discussion started by: av_vinay
9 Replies

5. Programming

Generate a binary file using make

Hi, i am trying to create a makefile for my C++ program. when i say "make" it should generate the binary file. and when i say make clean, it should delete all the bins and libs. can u please help me out. (5 Replies)
Discussion started by: sunshine23
5 Replies

6. Shell Programming and Scripting

how to run script? call other script? su to another user? make a cron?

Good morning. I am searching for "how-to"'s for some particular questions: 1. How to write a script in HP-UX 11. 2. How to schedule a script. 3. How to "call" scripts from the original script. 4. How to su to another user from within a script. This is the basics of what the... (15 Replies)
Discussion started by: instant000
15 Replies

7. Shell Programming and Scripting

Timestamp in the filename

Hi i want to replace the previous time stamp with the current timsatp at the start of the file like 20090710_113354_FT0710a.txt this one to 20091111__113354_FT0710a.txt thanks in advance (3 Replies)
Discussion started by: Reddy482
3 Replies

8. Shell Programming and Scripting

Modify script to generate a log file

I've seen several examples of scripts in thise forum about having a script generate a log file. I have a script that is run from cron and that monitors a file system for a specfic filename(s) and then performs some actions on them. Normally I call this script from another script (which the one... (2 Replies)
Discussion started by: heprox
2 Replies

9. Shell Programming and Scripting

add timestamp to filename

Does anyone know how to add a timestamp to a file's name extension in a shell script? Please help.. (3 Replies)
Discussion started by: walterja
3 Replies

10. UNIX for Dummies Questions & Answers

how to add a timestamp to a filename?

whats going on guys. below is a script i made and am just curious if there is a "time stamp" command. so i can set the timestamp in a filename. #! /bin/ksh # # This scripts takes a list of files in the INDIR variable and compairs it to a list of files that are open in the same directory.... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question