Hello
I'm trying to write simple script to delete archive logs for RMAN, unfortunately it's not working, I tried two way to do that:
#!/bin/ksh
echo "Start ....."
rman target=/ << EOF
RUN {
delete force noprompt archivelog until time 'sysdate-10';
}
EXIT;
EOF
echo "END ..."
echo `date`
the other way was
I wrote RMAN commands in separate file:
vi /scriprts/rman_c.txt
RUN {
delete force noprompt archivelog until time 'sysdate-10';
}
EXIT;
Then I called those commands from shell script
#!/bin/ksh
echo "Start ....."
rman target=/ @rman_c.txt
echo "END ..."
echo `date`
When I run the script it will work in command line, but when I use it as crontab job it will never connect to RMAN
the output log for crontab job script will be:
cat /tmp/log.txt
Start .....
END ...
Mon Mar 4 12:09:01 AST 2013
Do you get any errors? Running in 'cron' is not like logging in, more like running "ssh user@ost ksh". Your environment is not set up, so commands that work interactively or with a controlling terminal may not run. Usually, the script starts with something like ". ./.profile </dev/null >/dev/null 2>&1" to get environment, but if you need a tty, tougher, maybe "ssh -tt localhost ' ./.profile </dev/null >/dev/null 2>&1;cmd args ....".
Also, 'cron' can discard or email stdout and stderr, so redirect them right off in the crontab line, and maybe everything inside the script as well:
(...) >>cron_xxx_log.$( date '+%Y-%m-%d' ) 2>&1
Be careful as '%' is meta in crontab, so it is hard to use date there. I wrote a 'date' wrapper that used '~' for '%', just for 'cron'. If the scripts that cron calls all immediately redirect their logging, you can use a shared log for all crontab lines that is fixed, as it is normally empty. Set up good logging. You deserve it. It is a great investment in your development speed (fast debug) and future (prod support much later).
In addition to what DGPickett said, I would suggest to be explicit regarding the absolute path to the rman executable:
export the ORACLE_HOME variable and use "$ORACLE_HOME"/bin/rman
instead of just rman
.
On some Linux distributions, for example, you may have more than one executable file named rman
(you could have /usr/bin/X11/rman
for instance).
Yes, the same environment that worked it interactively, including a $PATH to the right rman and any $OTHER_THINGS like that.
try souring the ~/.profile for oracle user and as suggested by radoulov, DHPickett , use absolute path names.
Thanks a lot everybody for help.
It works after I used as below
.
less /export/home/oracle/Tools/rman0.ksh
/export/home/oracle/.profile
rman target / nocatalog @/export/home/oracle/Tools/rman0.rcv log="/u04/rmanbkup/log/rman-`date +%y%m%d`.log"
Welcome to the land of ssh, cron and at. When you log in, you are given environment by layers of files. If a pid runs not logged in, it has to build environment. Full absolute paths are just a weak stopgap for a bigger symptom. What if you need a dynamic lib*.so and your LD_LIBRARY_PATH is unset? Oracle has its own env needs.
If you want something simple, you can just "set >.runtime_env" and clone your environment. Some of it may be too specific to your login to be appropriate; it will be 'too much'. I do 'if' sections in my .profile for 'only if tty present', 'only if X', 'only if ksh' or 'only if bash', so it prunes itself down in cron, sh, ssh.