including yesterdays date in cron.tab

nnCron and nnCron LITE discussion

including yesterdays date in cron.tab

Postby stefanb » Wed, 22 Mar 2006, 18:08

Hi there!

I am using nncronlite to automatically gzip web log files. How can I include yesterdays date in a filename?

For example:

The task is running everyday and should unzip yesterdays logfile. The logfiles are named by date, e.g access-2006-03-21.log.gz for yesterdays log.
How can I tell cron to fill in the file name automatically each day? Variables like %DD% won't work since its today's value.

Thanks for your help!

Stefan
stefanb
 
Posts: 3
Joined: Wed, 22 Mar 2006, 18:02

Postby Valery_Kondakoff » Wed, 22 Mar 2006, 18:34

This is rather easy using nnCron and it's advanced syntax, but is possible as well in nnCron LITE using Forth programming and some predefined nnCron Forth words which are available in nnCron LITE.

See nnCron docs for words DAY+ ( http://www.nncron.ru/help/EN/commands/date_time.htm#DAY ) and Year@, Mon@, Day@ ( http://www.nncron.ru/help/EN/commands/d ... tm#current ).

Thus, first you put the numerical value of current year, month and day to a stack, then add '-1' to get the yesterday values.

Then you may want to convert the values on a stack to a strings ( http://www.nncron.ru/help/EN/add_info/f ... htm#n_to_s )...

HTH!
CU
VK
User avatar
Valery_Kondakoff
Site Admin
 
Posts: 769
Joined: Thu, 01 Jul 2004, 20:49
Location: Moscow, Russia

Postby stefanb » Wed, 22 Mar 2006, 19:09

Hi,

sorry, but I don't seem to understand how to do it exactly. Do I have to define a new "word" in Forth which tells me, e.g. yesterdays filename based on todays date? And then I can put up something like this in cron.tab

30 2 * * * unzip.exe e d:\logs\!yesterdayslog

being !yesterdayslog the newly defined word which gives something like access-2006-03-21.log.gz

How do I define this new word?

Seems rather complicated to me, since I never heard of Forth before....

Thanks

Stefan



[quote="Valery_Kondakoff"]This is rather easy using nnCron and it's advanced syntax, but is possible as well in nnCron LITE using Forth programming and some predefined nnCron Forth words which are available in nnCron LITE.

See nnCron docs for words DAY+ ( http://www.nncron.ru/help/EN/commands/date_time.htm#DAY ) and Year@, Mon@, Day@ ( http://www.nncron.ru/help/EN/commands/d ... tm#current ).

Thus, first you put the numerical value of current year, month and day to a stack, then add '-1' to get the yesterday values.

Then you may want to convert the values on a stack to a strings ( http://www.nncron.ru/help/EN/add_info/f ... htm#n_to_s )...

HTH![/quote]
stefanb
 
Posts: 3
Joined: Wed, 22 Mar 2006, 18:02

Postby Valery_Kondakoff » Thu, 23 Mar 2006, 18:53

There is a word 'yesterday' which calculates the yesterdays date:
Code: Select all
CREATE MY_FILENAME 256 ALLOT
: yesterday
    Year@ Mon@ Day@ -1 DAY+ ROT
    S" access-" MY_FILENAME PLACE
    S>D <# # # #> MY_FILENAME +PLACE
    S" -" MY_FILENAME +PLACE
    SWAP S>D <# # # #> MY_FILENAME +PLACE
    S" -" MY_FILENAME +PLACE
    S>D <# # # #> MY_FILENAME +PLACE
    S" .log.gz" MY_FILENAME +PLACE
    MY_FILENAME COUNT
;


Place the code in yesterday.spf and add 'INCLUDE yesterday.spf' in cron.ini (don't forget to restart nnCron LITE service). Now you can use 'yesterday' in your tasks. For example:
Code: Select all
* * * * * cmd.exe /c echo %yesterday% > test.txt
CU
VK
User avatar
Valery_Kondakoff
Site Admin
 
Posts: 769
Joined: Thu, 01 Jul 2004, 20:49
Location: Moscow, Russia

Postby Valery_Kondakoff » Thu, 23 Mar 2006, 18:59

Most simple version.
yesterday.spf:
Code: Select all
CREATE MY_FILENAME 256 ALLOT
: yesterday
    Year@ Mon@ Day@ -1 DAY+ ROT
    S>D <# # # #> MY_FILENAME PLACE
    S" -" MY_FILENAME +PLACE
    SWAP S>D <# # # #> MY_FILENAME +PLACE
    S" -" MY_FILENAME +PLACE
    S>D <# # # #> MY_FILENAME +PLACE
    MY_FILENAME COUNT
;


cron.tab
Code: Select all
* * * * * cmd.exe /c echo access-%yesterday%.log.gz > test.txt
CU
VK
User avatar
Valery_Kondakoff
Site Admin
 
Posts: 769
Joined: Thu, 01 Jul 2004, 20:49
Location: Moscow, Russia

Postby stefanb » Thu, 23 Mar 2006, 19:12

Excellent!

I have tried for myself but did not succeed so far. You saved me a lot of work. Thank you very much!

Bye

Stefan
stefanb
 
Posts: 3
Joined: Wed, 22 Mar 2006, 18:02


Return to nnCron forum (English)

Who is online

Users browsing this forum: No registered users and 3 guests

cron