LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Red Hat (http://www.linuxquestions.org/questions/red-hat-31/)
-   -   cron as user 'oracle' works for some tasks, not for others (http://www.linuxquestions.org/questions/red-hat-31/cron-as-user-oracle-works-for-some-tasks-not-for-others-4175447024/)

charkolis 01-24-2013 07:54 AM

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.

jackd1000 01-24-2013 08:01 AM

you need to show the cron script in question

druuna 01-24-2013 08:05 AM

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


charkolis 01-24-2013 08:17 AM

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

druuna 01-24-2013 08:42 AM

Quote:

Originally Posted by charkolis (Post 4876753)
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.

druuna 01-24-2013 09:09 AM

Quote:

Originally Posted by charkolis (Post 4876753)
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.

jackd1000 01-24-2013 09:15 AM

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

charkolis 01-24-2013 11:33 AM

Quote:

Originally Posted by druuna (Post 4876751)
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.

thesnow 01-24-2013 11:44 AM

That should go in each script you run from cron, not the crontab itself.

chrism01 01-24-2013 09:10 PM

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


All times are GMT -5. The time now is 02:38 PM.