LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices

Reply
 
Search this Thread
Old 01-24-2013, 07:54 AM   #1
charkolis
LQ Newbie
 
Registered: Jan 2013
Distribution: RHEL 5 64bit
Posts: 3

Rep: Reputation: Disabled
Question cron as user 'oracle' works for some tasks, not for others


Hi,
I'm an oracle dba and have been pulling my hair out trying to figure out why certain cron jobs are not working while others do. It has to be an environment issue but I still can't figure it out. I have two cron jobs that work fine, each one calling a .ksh script and then appending >> the output to a log file. Both scripts call sqlplus and perform a few tasks within the database.

Below those two tasks, I have been trying to clean up my log files by zipping or tar them (neither work), then in a separate cron delete log files older than an week. So I'm trying to use zip,gzip,tar,find,rm (that I can think of). All of these tasks can be done outside of cron. At the very top of my crontab I execute the .bash_profile for oracle via '. /home/oracle/.bash_profile'.

After googling the problem, I tried to locate any logs of the problem and all I see is the actual command, no ouput past that point. For example, I would see that at 2300 my tar cron statement was executed but nothing other than that. Nothing gets done and nothing gets written to a log file even if I try to append the output to a file. Any help appreciated. RHEL5 64bit, oracle is in cron.allow.
 
Old 01-24-2013, 08:01 AM   #2
jackd1000
Member
 
Registered: Jul 2007
Posts: 67

Rep: Reputation: 15
you need to show the cron script in question
 
Old 01-24-2013, 08:05 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi and welcome to LQ!

The most common problem when using cron to run a script: the environment isn't parsed when running from cron.

You only post a global problem sketch and no details, so I cannot point you to something specific.

One way to make sure that your (user) profile is parsed when running a script from cron is doing something like this:
Code:
#!/bin/ksh

# running from cron?
[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.profile

# rest of the code
 
Old 01-24-2013, 08:17 AM   #4
charkolis
LQ Newbie
 
Registered: Jan 2013
Distribution: RHEL 5 64bit
Posts: 3

Original Poster
Rep: Reputation: Disabled
Ok here's the crontab I have:

00 23 * * * /opt/oracle/blah/blah/name.ksh >> /log/location/output.log
00 22 * * * /opt/oracle/blah/blah/dynamic.ksh >> /log/location/output1.log
00 02 * * 0 tar -czf tracesid1_$(date +%y%m%d).tar.gz /opt/app/oracle/trace/SID1_*
15 02 * * 0 tar -czf coresid1_$(date +%y%m%d).tar.gz /opt/app/oracle/trace/cdmp*
30 02 * * 0 find /opt/app/oracle/trace/ -mtime +7 -name "*.tr*" -exec rm -rf {} \;

The first two work, the last 3 do nothing. Thanks again for any help.
Forgot the first line, this is at the top:
* * * * * . /home/oracle/.bash_profile

Last edited by charkolis; 01-24-2013 at 08:20 AM.
 
Old 01-24-2013, 08:42 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by charkolis View Post
Ok here's the crontab I have:

00 23 * * * /opt/oracle/blah/blah/name.ksh >> /log/location/output.log
00 22 * * * /opt/oracle/blah/blah/dynamic.ksh >> /log/location/output1.log
00 02 * * 0 tar -czf tracesid1_$(date +%y%m%d).tar.gz /opt/app/oracle/trace/SID1_*
15 02 * * 0 tar -czf coresid1_$(date +%y%m%d).tar.gz /opt/app/oracle/trace/cdmp*
30 02 * * 0 find /opt/app/oracle/trace/ -mtime +7 -name "*.tr*" -exec rm -rf {} \;

The first two work, the last 3 do nothing. Thanks again for any help.
Forgot the first line, this is at the top:
* * * * * . /home/oracle/.bash_profile
The % sign is special when seen from cron. Try escaping them:
Code:
00 02 * * 0 tar -czf tracesid1_$(date +\%y\%m\%d).tar.gz /opt/app/oracle/trace/SID1_*
15 02 * * 0 tar -czf coresid1_$(date +\%y\%m\%d).tar.gz /opt/app/oracle/trace/cdmp*
Not sure why the third one doesn't work (probably something similar as the other 2).

In general: Don't put commands + options in cron. Put the command and options in a wrapper script and use that wrapper script in cron.

Last edited by druuna; 01-24-2013 at 08:43 AM. Reason: fixed typo
 
1 members found this post helpful.
Old 01-24-2013, 09:09 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Quote:
Originally Posted by charkolis View Post
Forgot the first line, this is at the top:
* * * * * . /home/oracle/.bash_profile
There's no point in doing this, the profile will not be used by the other entries.
 
Old 01-24-2013, 09:15 AM   #7
jackd1000
Member
 
Registered: Jul 2007
Posts: 67

Rep: Reputation: 15
I've always had problems with cron trying to run full commands, including the arguments.

My strategy has been - create shell wrapper, address with full path, and use the profile within the wrapper and NOT the cron file.

jack
 
Old 01-24-2013, 11:33 AM   #8
charkolis
LQ Newbie
 
Registered: Jan 2013
Distribution: RHEL 5 64bit
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Hi and welcome to LQ!

The most common problem when using cron to run a script: the environment isn't parsed when running from cron.

You only post a global problem sketch and no details, so I cannot point you to something specific.

One way to make sure that your (user) profile is parsed when running a script from cron is doing something like this:
Code:
#!/bin/ksh

# running from cron?
[[ "`/usr/bin/tty`" == "not a tty" ]] && . ~/.profile

# rest of the code
If I enter this, it won't let me save the crontab. I copied it into notepad just to make sure I had everything right and it still won't let me save it.
 
Old 01-24-2013, 11:44 AM   #9
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 126

Rep: Reputation: 28
That should go in each script you run from cron, not the crontab itself.
 
Old 01-24-2013, 09:10 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
In short:

1. don't put cmds in crontab, put shell/perl/whatever scripts that do what you want.
2. cron env is minimal; in the shell scripts, specify complete paths to all cmds and files OR at the top of the script, explicitly call the relevant env setup file OR hard code env
3. use built-in tool logrotate to deal with log file trimming (any similar files also) http://linux.die.net/man/8/logrotate
4. as above, '* * * * * . /home/oracle/.bash_profile' is completely pointless
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Scheduled Tasks / Cron not working right? Lossenelin Linux - General 2 05-23-2011 11:02 PM
running kde apps as cron tasks jason.farnon Linux - Desktop 3 02-17-2007 11:39 AM
Cron - doesn't seem to run scheduled tasks robintw Linux - General 11 11-25-2005 02:53 AM
All cron tasks fail Ateo Linux - Software 2 06-11-2005 02:34 PM
cron tasks MaverickApollo Linux - Software 1 02-02-2004 02:06 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration