LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab not executing shell when date is added (http://www.linuxquestions.org/questions/linux-newbie-8/crontab-not-executing-shell-when-date-is-added-929435/)

Jakkie 02-15-2012 05:15 AM

Crontab not executing shell when date is added
 
Hi Guys,

I created a shell which work well when I run it from the command line:

# ./test.sh

I added a time stamp to record the execution like this:

# ./test.sh > log`date +%Y%m%d-%H%M%S`

This also works well. Every line executed inside the script is recorded. No problem here...

However, when I want to schedule this in crontab, it does not work if I add the log part at the back with the date. I only works if I do this in crontab:

* * * * * ./test.sh

Or

* * * * * ./test.sh > log

But crontab does not even execute the command if I do this:

* * * * * ./test.sh > log`date +%Y%m%d-%H%M%S`

...where the date part is added at the back of the file.

Do you guys know if there is a limitation in crontab for not running certain commands?

acid_kewpie 02-15-2012 05:17 AM

I'd put that inside the shell script TBH, it'd be nicer in there. If not, use the full path to data, and replace the `backticks` with $(brackets) as that's much nicer.

Jakkie 02-15-2012 05:26 AM

Hi Acid_Kewpie, Thanks! Are you saying that I should create a second script, just to launch the first script? For instance, in crontab I will have an entry like:

* * * * * ./run.sh

... to start the first script and inside the run.sh, I put:

./test.sh > log`date +%Y%m%d-%H%M%S`

Something like this?

J

acid_kewpie 02-15-2012 05:27 AM

No, I mean you should put the logging inside test.sh. But you don't have to. I just find cron a hell of a lot easier if you keep the commands simple. Do check what I said about the path though.

Jakkie 02-15-2012 05:30 AM

I see, yes I agree. Keep the command simple in cron is the best way. I'm not sure hove to add the logging inside the test.sh? How will I be able to record each step from within the script itself? Can you shed some light on this?

acid_kewpie 02-15-2012 05:31 AM

well I've no idea what's in there, and maybe it's not that feasible...

Jakkie 02-15-2012 05:40 AM

OK, thanks anyway for the help. Lets see if someone else has other suggestions to handle this beast... :)

acid_kewpie 02-15-2012 05:45 AM

that was a cue for you to show the script...

and have you tried putting in the full path to date or not?

Jakkie 02-15-2012 06:03 AM

Hi, yes I tried the date suggestion, but it did not work. Unfortunatly I cannot provide the script. There is confidential info inside the script like keys and other company related stuff which would not be ideal to share over the net. I tried executing the scripts from within another script as mentioned in my first reply and that works well. The log file is created perfectly with the timestamp. Thanks for you help.

Cheers,
J

colucix 02-15-2012 06:04 AM

The problem is that the % sign has a special meaning in crontab. From man 5 crontab:
Quote:

Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the first %
will be sent to the command as standard input.
As suggested by the man page, the solution is to escape the percent signs in the date format, e.g.
Code:

0 * * * * /absolute/path/to/test.sh > log_$(date +\%Y\%m\%d-\%H\%M\%S) 2>&1
Hope this helps.

Jakkie 02-15-2012 06:07 AM

Ah, great. Let me try that quickly. Will revert back shortly... PS: I dont need the 2>&1 part at the end, right? That's just to trap errors, correct?

colucix 02-15-2012 06:08 AM

Quote:

Originally Posted by Jakkie (Post 4602832)
PS: I dont need the 2>&1 part at the end, right? That's just to trap errors, correct?

Yes (to redirect standard error to standard output). I've written a sample crontab as I'm used to.

Jakkie 02-15-2012 06:12 AM

Hi Colucix, This is exctly what I wanted. I added your suggestion into cron and it is creating the log files perfectly. Thanks for the help. I will mark this thread as SOLVED. Cheers guys!

colucix 02-15-2012 06:17 AM

You're welcome! :)

acid_kewpie 02-15-2012 06:18 AM

You don't *need* it, but if it does error, you'll most likely be emailed the errors, which can be messy and unwanted. you set that off every minute and eventually you have a 500gb root mail file killing your system.


All times are GMT -5. The time now is 11:25 AM.