shell script is not working in cron job
Hi All,
i have created the shell script to fetch the DB details,if i run manually it's working but when i am running with cron it's not fetching the DB details.
Please suggest what need's to be done to fetch the data through cron job
Responses
Hi,
Very common problem.
Cron tasks are non-interactive and without having most of environment variables that a normal login session has.
In your case, make sure that all commands have the proper path to them.
Typically, here is how it can be done in a safe manner:
#!/bin/sh
PATH=/usr/bin:/sbin:; export PATH
... the rest of the script
Regards,
Dusan Baljevic (amateur radio VK2COT)
You have added sqlplus to PATH instead of the directory of sqlplus. Please use:
PATH=/usr/bin:/sbin:/oracle/base/11.1.0/db/client/bin
export PATH
To improve readability of your code in this forum, please surround your code with separate lines containing three tilde ('~') characters.
Hi,
This error is also related to your incomplete environment. The clue is here:
Error 6 initializing SQL*Plus SP2-0667: Message file sp1.msb not found SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
At a minimum, you need to add ORACLE_HOME into your script.
You might be better off to add this line into your cron script:
. /whateverdir/.profile
or
. /whateverdir/.bashrc
... do it for the profile that your Shell uses.
Regards,
Dusan Baljevic (amateur radio VK2COT)
Hi Ibrahim.
I think you missed a very tiny piece of information about sou5rcing profile.
It is not:
/home/ibrahim/.bashrc
This is valid:
. /home/ibrahim/.bashrc
And this is valid too:
source /home/ibrahim/.bashrc
"." is synonymous with "source" in Bash, but not in POSIX Shell, so you should use "." if your script is run by /bin/sh.
Regards,
Dusan Baljevic (amateur radio VK2COT)
Are you sure the script is working correctly when you invoke it from the command line?
Any script that starts with "!/bin/sh" instead of "#/bin/sh" produces a message: "!/bin/sh: No such file or directory" before continuing.
Your here-document construct will continue reading your script until a line containing ONLY the string "EOF" is read. You have appended an echo to the line with the marker. I created a similar script and it produces the following message:
sh: warning: here-document at line <line_number> delimited by end-of-file (wanted `EOF')
To direct the output of the sqlplus command and its exit status to a file, you will need something like:
$Cred >/tmp/Crontest <<EOF
...
EOF
echo $? >>/tmp/Crontest
Hi,
You still seem to be having a problem setting environment variables.
Ensure to have you ORACLE_HOME/sqlplus, ORACLE_HOME/sqlplus/mesg directories allowed for read and execute, and all the ORACLE_HOME/sqlplus/mesg/*.msb files readable when trying to use sqlplus with a non-Oracle owner user.
Is the cron job run as root or ibrahim?
If the cronjob is set for user root, then you would need to set it correctly. Something like:
5 7 * * * /bin/su - ibrahim -c "/home/user/Test.sh 2>&1"
Regards,
Dusan Baljevic (amateur radio VK2COT)
Hi,
When you run the Shell script from the command-line (manually), what are the environment variables that are set?
To verify, run something as simple as:
$ env
Then you need to concentrate on which ones you are missing in your setup via cron task.
Also, can you add more logging in your cron job ( I am not sure that you really run your task every minute round the clock):
* * * * * /home/ibrahim/Test.sh 2> somelogfile.txt 2>&1
Regards,
Dusan Baljevic (amateur radio VK2COT)
This strongly suggests that your ORACLE_HOME was not set up.
Try this in the script:
#!/bin/bash -x
PATH=/usr/bin:/sbin:/usr/sbin:/oracle/base/11.1.0/db/client/bin;export PATH
source /home/ibrahim/.bashrc
ORACLE_HOME=/oracle/base/11.1.0/db/client; export ORACLE_HOME
Cred=`cat < /home/ibrahim/Login`
env
RETVAL=` $Cred <<EOF
SET LINESIZE 30000
SET PAGESIZE 50000
SET feedback off
select * from emp;
EXIT;
EOF`
echo "$RETVAL" >> /tmp/Crontest
And then run the cron job again:
* * * * * /usr/bin/bash /fispss/fpsweb/Scripts/Test.sh 2> /tmp/TestFile.txt 2>&1
Regards,
Dusan Baljevic (amateur radio VK2COT)
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
