LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cron job question (https://www.linuxquestions.org/questions/linux-general-1/cron-job-question-531668/)

kashyapvirgo 02-23-2007 08:51 AM

cron job question
 
I just try to put date for log file name with my backup script in cron job but it doesnt work.

here is line

0 1 * * 1-6 /usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date +%y%m%d-%a`-log.txt

but above text does not generate any file name with DATE-log.txt.if i put just log.txt it gives output in file called log.txt.


When i do it manually it works completely fine either way.

wjevans_7d1@yahoo.co 02-23-2007 09:10 AM

Can you give the complete and exact crontab line that does work? The one where you "just put log.txt"?

andrews-mark 02-23-2007 10:35 AM

I'm not sure exactly why it is doing what it is doing but I encountered the same problem as you. It is easy to fix though.

As an experiment, I set up the following cron job
Code:

*/2 * * * * echo `date` > junk/`date +%y%m%d-%a-%H-%M-%S`-log.txt
so that every 2 mins it would write the current date to a file in my junk directory with name made up from the present date and time.

It did not work. I'm not sure why. Maybe someone smarter than me will tell me why.

Anyhow, it is easy to fix. I just put
echo `date` > junk/`date +%y%m%d-%a-%H-%M-%S`-log.txt
into a file called prog.sh, gave it executable permission and created the cron job
Code:

*/2 * * * * /home/andrews/prog.sh
it works just as it should
Code:

andrews@centurian ~/junk> ls -1t
070223-Fri-16-32-01-log.txt
070223-Fri-16-30-01-log.txt
070223-Fri-16-28-01-log.txt
070223-Fri-16-26-02-log.txt
070223-Fri-16-24-01-log.txt
070223-Fri-16-22-01-log.txt
070223-Fri-16-20-01-log.txt
070223-Fri-16-18-01-log.txt
andrews@centurian ~/junk> cat 070223-Fri-16-32-01-log.txt

-mark


Fri Feb 23 16:32:01 GMT 2007


colucix 02-23-2007 11:44 AM

The solution from andrews-mark is fine: better to put commands inside a script and execute that script as cron job. This will avoid (almost) any problem with the cron environment and a lot of headache! ;)
BTW, the problem you encountered is due to the special meaning of the % sign in crontabs. Putting a % in a crontab entry, means to start a newline. Indeed, the standard error from cron reports
Code:

/bin/sh: -c: line 0: unexpected EOF while looking for matching '`'
/bin/sh: -c: line 1: syntax error: unexpected end of file

As you can see, the command has been splitted into two lines. In line 0 bash cannot find the matching closing quote. In line 1 bash expects something more because encountered a starting quote. In other words, crontab represent the command as
Code:

/usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date +
y%m%d-%a`-log.txt

Note that only the first % sign has the meaning of newline. I think (but not for sure) that the stuff after the first % is used as standard input from the previous command.
Now, let's see the workaround! To avoid this behaviour you have to simply escape every % sign with a backslash in front of it
Code:

0 1 * * 1-6 /usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date +\%y\%m\%d-\%a`-log.txt
Sorry for the long preamble! :)

kashyapvirgo 02-23-2007 12:02 PM

thanx for replies ...I will try it. i also found another suggestion whcih is to put bin/date instade of just date in file name.It will solve env variable problem.

kashyapvirgo 02-23-2007 12:06 PM

back slash before % works completely fine thanx :)

colucix 02-24-2007 01:07 AM

You're welcome! :) Just a little note about your last post! Is it true that issuing commands in crontab with their fullpath avoids some problems, since the crontab environment has a very limited PATH, usually /bin:/usr/bin. In the case of /bin/date this is totally not nedeed, because /bin is always in the PATH of cron. Not an error, anyway...


All times are GMT -5. The time now is 02:03 PM.