2008-01-07

Cron job with the log file

Today I need to write a cron job to make some sql queries automatically executed. Also a log file with the date is necessary according to my boss. :-)

So here is the solution:
---------------------------------------------------------------
# Date in Format yyyymmdd_hhmmss:
SQLNAME=/home/sybase/logs/table_`date +%Y%m%d`_`date +%H%M%S`.sql

# Every day at 23 o'clock the sql query will be executed
00 23 * * * root /opt/sybase/SYBSsa9/bin/dbisql -nogui -c "eng=enginename;dbn=dbname;uid=user;pwd=password;" /opt/sybase/SYBSsa9/bin/whatever.sql >> $SQLNAME
---------------------------------------------------------------

Some explainations:
  1. SQLNAME is a variable name. It consist two parts, the normal text and the date. Notice to that, please always enclose the date part in backticks ( ` ) , otherwise it will be not executed. This variable can be used later with the format $SQLNAME
  2. dbisql is the command line tool for Sybase. In whatever.sql you can write the sql queries you need.

1 comment:

EverGreen said...

I am redirecting the output of cron job to a log file. But, I want the log file to have date and timestamp, so that cron creates a new file each time it runs.

So, I used your solution. But, it did not worked.

This is what I had done:

CLCLOG = /home/glg/logs/cronlogs/clc_`date+%Y.%m.%d.%H.%M.%S`.log

15 * * * * /home/glg/bin/clc.sh >> $CLCLOG

The cron ran but created a log file by name - "clc_`date+%Y.%m.%d.%H.%M.%S`.log", instead of clc_2008.03.24.23.15.00.log.

Do you know what can be the problem here?