0

I wrote a shell script to automatically check the status of Oracle Data Guard and send it via mail.

If I execute the script manually it works like it should.

If I execute the script via a cronjob it just sends empty emails. This behaviour started only after we updated our servers from oracle linux 7 to oracle linux 8.

This is the script:

#!/bin/bash

#. ~/envBIPROD.sh
source /home/oracle/.bash_profile
source /home/oracle/envBIPROD.sh

dgmgrl -silent  user/pass >biprod.txt << EOF
show configuration;
show database p1biprod;
show database s1biprod;
EOF

cat biprod.txt | mail -r $(whoami).$(hostname -s)@hostname.tld -s "DATA GUARD Status biprod" [email protected]

This is the "envBIPROD.sh" script which sets the oracle home:

export ORACLE_SID=BIPROD1
export ORAENV_ASK=NO
. oraenv
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH

Also the file "biprod.txt" is just empty when the script is run by cron.

7
  • 2
    most cron problems are you assuming things about the permissions or the environment of the cron user. e.g. the PATH isn't what you think it is, some environment variable isn't what you think it is, or the exact user and group permissions of cron isn't what you think it is. For example, you seem to be assuming ORACLE_HOME is set when cron runs it. That's doubtful. A start would be running env in the script so you can see what your environment is. Commented Nov 22, 2023 at 10:59
  • I know that ORACLE_HOME is not set, thats why I execute envBIPROD.sh which invokes the oraenv utility to set the ORACLE_HOME. Commented Nov 22, 2023 at 12:34
  • 1
    keep looking, there is something different about the environment or permissions. Run id to see what username and groups you are in shell and in cron. Start a new bash with nothing env - bash, then source those scripts, and see if it still runs. Commented Nov 22, 2023 at 15:06
  • 1
    Standard cron job troubleshooting: capture errors & output from the job by adding something like >>/tmp/cronjob.log 2>&1 to the command in crontab (and if you have anything like 2>/dev/null, remove it -- that's hiding errors, which is the opposite of what you want). Then check the log after it runs and see if you see anything informative. Commented Nov 22, 2023 at 19:59
  • 1
    You need to put the 2>&1 after the >> /cloudfs/dba/scripts/data_guard_status_mail_BIPROD.log or it won't work. Commented Nov 23, 2023 at 9:51

1 Answer 1

2

Your script is probably running in a directory where it doesn't have permission to create or write to a file and so >biprod.txt is failing to create the file or to overwrite an existing file.

If biprod.txt is intended to only exist while your script is running then do this instead:

biprod=$(mktemp) || exit 1
trap 'rm -f "$biprod"; exit' EXIT

dgmgrl -silent  user/pass > "$biprod" 
...

cat "$biprod" | ...

though you almost certainly could be using < "$biprod" mail ... instead of cat "$biprod" | mail ...). It wouldn't hurt for you to add some printfs to report failures either.

Sign up to request clarification or add additional context in comments.

2 Comments

It is commented out because I wanted to see if it works with the source command, which basically should do the same. I dont think it has no permissions, because the cronjob creates a empty file , which it would not be doing if there was no permissions to this folder
You didn't mention that ~ expands to /home/oracle, I thought they were 2 different directories, I'll remove that part of my answer. Are you sure the crontab creates an empty file and it's not just that an empty file of that name exists which crontab doesn't have permission to write to or that crontab is trying to create one in a different directory? Have you checked the timestamp on biprod.txt to make sure that changes when the cron job runs? Have you tried replacing dgmgrl ...>biprod.txt << EOF ... EOF with echo 'hello world' > biprod.txt to see if THAT succeeds?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.