LinuxQuestions.org
Review your favorite Linux distribution.
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, 06: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, 07: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, 07:05 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 07: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 07:20 AM.
 
Old 01-24-2013, 07:42 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 07:43 AM. Reason: fixed typo
 
1 members found this post helpful.
Old 01-24-2013, 08:09 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 08: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, 10: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, 10:44 AM   #9
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
That should go in each script you run from cron, not the crontab itself.
 
Old 01-24-2013, 08:10 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,352

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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 10:02 PM
running kde apps as cron tasks jason.farnon Linux - Desktop 3 02-17-2007 10:39 AM
Cron - doesn't seem to run scheduled tasks robintw Linux - General 11 11-25-2005 01:53 AM
All cron tasks fail Ateo Linux - Software 2 06-11-2005 01:34 PM
cron tasks MaverickApollo Linux - Software 1 02-02-2004 01:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 08:05 PM.

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