Link to home
Start Free TrialLog in
Avatar of Kenya Westmoreland
Kenya WestmorelandFlag for United States of America

asked on

Best method to call another script after a program finishes successfully

I am an Oracle DBA that has databases sitting on a Linux VM (RHEL 7).  I have been tasked to copy backup files from one directory to another after the RMAN backup completes successfully. My logic is
1. Run backup
2. Search inside log for completion wording
3. Call program to start copy of files to another directory

Where I am stuck is how to script #2. Psuedo code
Search in database.log for 'finished'
if 'finished' found then call copy script
else exit


Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Welcome to the site!

How are you executing RMAN?

You should just be able to check the exit code of the rman command.

To see it, immediately after the rman command:  echo $?

From a shell prompt, create some rman command you know will fail (there is no hello parameter):
rman hello
echo $?
The suggestion slightbvw dealing with the status of the run $? is valid in bash and sh
but it can be masked depending on as was pointed out the backup script which hopefully includes sanity checks to make sure the backup run status within the script is detected.

there are many way to achieve the same thing, how is it being used in your environment.
1) as slightvw points out, you can modify the existing script and add lines after the backup run, to test the exit code. and then call your script.
a) the issue with that is that if you have many databases being backed up, the DB by DB might be unwise as you would need to pass to the secondary script which DB was backed up.

Since when I do go through the question in full and read experts comments in full and then start typing, a thought has crossed ..

You use the word copy. If you need two copies of the backup.
might the following be something to consider
https://blog.dbi-services.com/easily-manage-dual-backup-destination-with-rman/

possibly the existing location will be managed by a script to maintain X number of prior backups
while the second destination could be managed for a longer retention duration.

Purpose may help clarify.
Avatar of Kenya Westmoreland

ASKER

I am running RMAN through a script by it calling a .rcv file. I will try the return code at work tomorrow, so something like this

bash script
rman target / @rman_file.rcv &> rman.log &
wait
echo $?
if echo $? does not equal 1
then copy script



@Arnold- We just need a second copy of the backups in another location. They want the same files to exist on our recovery site and the mount point. 
You are sending the task to the background, the status code you will get is from the wait.

Rman_status=$(Rman target / @rman_file.rcv &> Rman.log ; echo $?)

If test "$?" -eq '0' ; then
  Copyfiles.sh
Fi

Seems redundant to send the task to background while invoking wait.
Hi Kenya,
I'm not familiar with the "rman" command, nor the "/" and "@..." arguments you're passing to it.
However, regarding this line:
    "if echo $? does not equal 1"
Would having "equal 0" instead of "not equal 1", be better, to handle other failure return codes?  I've assumed "equal 0" would be better in my code below, but feel free to change that as required, as I don't know what kinds of return codes rman gives.
I would have thought something like this would work:
rman target / @rman_file.rcv &> rman.log
[[ $? -eq 0 ]] && copyfiles.sh

Open in new window

or if you need to retain the return code for other purposes...
rman target / @rman_file.rcv &> rman.log
RC=$?
[[ $RC -eq 0 ]] && copyfiles.sh

Open in new window

If you want your file copying code to be contained in the same script then maybe something like this:
rman target / @rman_file.rcv &> rman.log
[[ $? -ne 0 ]] && exit      # Or "exit 1" or whatever code you want to return
# Put file copying code here

Open in new window

I haven't been able to test that exactly as is, since I don't have rman, but I tested most of it with other commands and it seemed to work.

Hi arnold,
Have you tested your code?
Rman_status=$(Rman target / @rman_file.rcv &> Rman.log ; echo $?)
If test "$?" -eq '0' ; then
  Copyfiles.sh
Fi

Open in new window

I see several issues with your code, including:
- Syntax errors - "If" should be "if" and "Fi" should be "fi".
- No need to 'quote' the "$?" or '0' because "-eq" compares integers.
- Your 'If test "$?"'' will succeed as long as the assignment of Rman_status succeeds, so $Rmain_status could be 0, but the test still succeeds.  Maybe you meant 'If $Rman_status -eq 0'.
(If you haven't tested your code, I suggest you state that, because otherwise it could cause extra confusion to readers.  As I think I've said to you before, it might be better to wait until you have access to a suitable computer so you can test it yourself before posting it.)
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.