Operating System - HP-UX
1752311 Members
5617 Online
108786 Solutions
New Discussion юеВ

Re: attaching time stamp on out put file

 
SOLVED
Go to solution
diwakar_4
Frequent Advisor

attaching time stamp on out put file

Hi All,

I am trying to ececute one script from crontab. I want to get the out in one file.
But i want file should have date and time as well.

My crontan setting is like:
00,15,30,45 * * * * /usr/sym/syst/menus/yqclear.scr 1>/tmp/log 2>&1

I want to attach date and time as well with log file.
like

00,15,30,45 * * * * /usr/sym/syst/menus/yqclear.scr 1>/tmp/log_`date +%Y%m%d%H%M` 2>&1


But it is not working. I am getting file :
log_200811071116.

Can any one please help me how to do this?

Thanks
14 REPLIES 14
Ganesan R
Honored Contributor

Re: attaching time stamp on out put file

Hi,

Do you want the date and time should be inside the output file or in the output file name?

how should your output file look like?
Best wishes,

Ganesh.
Dennis Handly
Acclaimed Contributor

Re: attaching time stamp on out put file

I'm confused. Do you want the timestamp IN the file or in the filename? Or both?
... DATE=$(date +%Y%m%d%H%M); (echo $DATE; \
/usr/sym/syst/menus/yqclear.scr) > tmp/log_$DATE 2>&1
Dennis Handly
Acclaimed Contributor

Re: attaching time stamp on out put file

Perhaps this is your problem. You must quote "%" in a crontab:
... DATE=$(date +\%Y\%m\%d\%H\%M); (echo $DATE; \
/usr/sym/syst/menus/yqclear.scr) > tmp/log_$DATE 2>&1
Steven Schweda
Honored Contributor

Re: attaching time stamp on out put file

The "date" format string can include plain
text as well as real date formatting
directives:

bash$ date +/tmp/log_%Y%m%d%H%M
/tmp/log_200811070540

bash$ echo fred > ` date +/tmp/log_%Y%m%d%H%M `
bash$ ls -l /tmp/log_*
-rw-r----- 1 SMS 40 5 Nov 7 05:45 /tmp/log_200811070545
bash$ cat /tmp/log_200811070545
fred
diwakar_4
Frequent Advisor

Re: attaching time stamp on out put file

Hi dennis,

I want date and time be as a part of file name.

it is giving proper file name but out is nothing but date value.

Here i am executing clear.scr which contains code:

echo " Welcome ".
My log file should show this message when i open the file(log_200811071206). But it shows value as 200811071206.


06 12 * * 1-5 DATE=$(date +\%Y\%m\%d\%H\%M); (echo $DATE; \ /usr/symology/cle
ar.scr) > /usr/symology/log_$DATE

Please suggest..
Ganesan R
Honored Contributor

Re: attaching time stamp on out put file

Hi,

# echo "welcome" 1>/tmp/log_`date +%Y%m%d%H%M` 2>&1
# ll /tmp/log*
-rw-rw-rw- 1 root sys 8 Nov 7 06:25 /tmp/log_200811070625
# more /tmp/log_200811070625
welcome
#
Best wishes,

Ganesh.
Patrick Wallek
Honored Contributor

Re: attaching time stamp on out put file

>> But it shows value as 200811071206.

Well, that IS the date and time from your command.

You did:

/tmp/log_`date +%Y%m%d%H%M`

So, from the 'date' command you get:

%Y = 2008 (the year)
%m = 11 (the month)
%d = 07 (the day)
%H = 12 (The hour)
%M = 06 (The minutes)

I'm not sure what else you want. Your explanations are not very clear to me.
Dennis Handly
Acclaimed Contributor

Re: attaching time stamp on out put file

>I want date and time be as a part of file name. it is giving proper file name but output is nothing but date value.

Once you get the log filename, you have to put what you want into the file.
You can adjust that echo I suggested, or remove it.
Ninad_1
Honored Contributor
Solution

Re: attaching time stamp on out put file

Hi,

What Dennis has suggested is the solution for you.

00,15,30,45 * * * * /usr/sym/syst/menus/yqclear.scr 1>/tmp/log_$(date "+\%Y\%m\%d\%H\%M") 2>&1

will do in one line.

You could have looked at /var/adm/cron/log file - for what cron is actually executing (means what it understands as the command to be executed).
This would have given you a clue where exactly you are going wrong.

Regards,
Ninad