LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2012, 04:15 AM   #1
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Rep: Reputation: Disabled
Talking 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?
 
Old 02-15-2012, 04:17 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 02-15-2012, 04:26 AM   #3
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-15-2012, 04:27 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 02-15-2012, 04:30 AM   #5
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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?
 
Old 02-15-2012, 04:31 AM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well I've no idea what's in there, and maybe it's not that feasible...
 
Old 02-15-2012, 04:40 AM   #7
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
OK, thanks anyway for the help. Lets see if someone else has other suggestions to handle this beast...
 
Old 02-15-2012, 04:45 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
that was a cue for you to show the script...

and have you tried putting in the full path to date or not?
 
Old 02-15-2012, 05:03 AM   #9
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-15-2012, 05:04 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 02-15-2012, 05:07 AM   #11
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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?
 
Old 02-15-2012, 05:08 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Jakkie View Post
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.
 
Old 02-15-2012, 05:12 AM   #13
Jakkie
LQ Newbie
 
Registered: Jan 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
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!
 
Old 02-15-2012, 05:17 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You're welcome!
 
Old 02-15-2012, 05:18 AM   #15
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Crontab not executing specific script abdoullah Linux - General 3 06-17-2011 02:15 PM
Seems crontab is not executing script... jonaskellens Linux - Newbie 3 11-18-2010 08:20 AM
crontab job not executing.... mia_tech Linux - Newbie 6 08-31-2009 02:42 PM
Command for executing C program in crontab bhandu Linux - Newbie 1 07-14-2007 12:54 PM
Naming files with date added jmorris8 Linux - Newbie 4 02-23-2007 01:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration