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. |
Can you give the complete and exact crontab line that does work? The one where you "just put log.txt"?
|
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 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 Code:
andrews@centurian ~/junk> ls -1t |
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 '`' Code:
/usr/BackupScriptsForCronjob/CCFullBackup.sh > /usr/BackupScriptsForCronjob/`date + 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 |
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.
|
back slash before % works completely fine thanx :)
|
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. |