shell script is not working in cron job

Latest response

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)

Same PATH exported in the script but still it's not able fetch the data from DB

Hi Ibrahim,

Show us the full crontab entry, also the mail report of the cronjob if possible.

Regards,

Jan Gerrit Kootstra

Hi,

Cron Tab Entry: * * * * * sh /home/user/Test.sh

After exporting the PATH, Now i am facing the sqlplus command not found.

Script:

!/bin/bash

PATH=/usr/bin:/sbin:; export PATH Cred =cat /etc/home/Login #### Login file has sqlplus -s username@DB/password RETVAL=$Cred << EOF Select * from Table EXIT; EOF echo "RETVAL" >/tmp/ouput

Manually it's giving the result without exporting the PATH.

Hi,

This is very obvious. sqlplus is not in your PATH.

From command-line check:

which sqlplus

... and then add it into the Shell script for cron. Maybe something like:

PATH=/usr/bin:/sbin:/usr/local/bin; export PATH

Regards,

Dusan Baljevic (amateur radio VK2COT)

Hi,

Tried same thing but still sqlplus command not found i am facing.

Script:

!/bin/bash

PATH=/usr/bin:/sbin:/oracle/base/11.1.0/db/client/bin/sqlplus;export PATH Cred =cat /etc/home/Login #### Login file has sqlplus -s username@DB/password RETVAL=$Cred << EOF Select * from Table EXIT; EOF echo "RETVAL" >> /tmp/ouput

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.

Thanks for the ~~~ suggestion.

Now my code is looking good. :-)

Hi Korteweg,

Thanks for the reply now i am not facing sql command problem, But still output is not generating with cron Please find the below user Mail information.

user@hostname Subject: Cron <user@hostname> sh /tmp/Test.sh Content-Type: text/plain; charset=UTF-8 Auto-Submitted: auto-generated Precedence: bulk X-Cron-Env: <XDG_SESSION_ID=74501> X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/1000> X-Cron-Env: <LANG=en_US.UTF-8> X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/user> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=username> X-Cron-Env: <USER=user> Message-Id: <20180522193801.986F722A3A@HOSTNAME> Date: Wed, 23 May 2018 01:08:01 +0530 (IST)

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

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 Baljevic,

I have tried still facing the same, Please check below my script and edit.

!/bin/sh

PATH=/usr/bin:/sbin:/usr/sbin:/oracle/base/11.1.0/db/client/bin;export PATH /home/ibrahim/.bashrc #### This line is not working ORACLE_HOME=/oracle/base/11.1.0/db/client #### ORACLE_HOME path got it in env output Cred=cat < /home/ibrahim/Scripts/Login RETVAL=$Cred <<EOF SET LINESIZE 30000 SET PAGESIZE 50000 SET feedback off select RRN from table; EXIT; EOF echo "$RETVAL" >> /tmp/Crontest

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)

Hi, I have used source command also in my script but still empty result is printing.

!/bin/sh
PATH=/usr/bin:/sbin:/usr/sbin:/oracle/base/11.1.0/db/client/bin;export PATH 
source /home/ibrahim/.bashrc
Cred=cat < /home/ibrahim/Scripts/Login 
RETVAL=$Cred <<EOF 
SET LINESIZE 30000 
SET PAGESIZE 50000 
SET feedback off 
select RRN from table; 
EXIT; 
EOF echo "$RETVAL" >> /tmp/Crontest

Oracle home path also i have appended in .bashrc file and compiled using the source but still no use I think somewhere missing minor point not getting where exactly.

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 Korteweg,

i real apologize for typo mistake in above script. Please check the exact script and copying .

Manually i am getting the data from table.

#!/bin/bash
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
Cred=`cat < /home/ibrahim/Scripts/Login`
RETVAL=` $Cred <<EOF
SET LINESIZE 30000
SET PAGESIZE 50000
SET feedback off
select * from emp;
EXIT;
EOF`
echo "$RETVAL" >> /tmp/Crontest

Hi Ibrahim,

Are you running the script as user ibrahim or as user oracle, if started manually?

If you use the user oracle, you must source ~oracle/.bashrc also you might need to run oraenv.

Regards,

Jan Gerrit Kootstra

Hi Kootstra,

Manually I am runnig the script with normal user ibrahim. That time i am getting the output but if when scheduling the job with cron not getting the output.

Hi Kootstra,

Below error there in user mail alert,

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.

please help me on this how to solve.

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 Baljevic,

All the files and directories have read and execution permission.

drwxr-xr-x  7 oracle oracle 4096 Oct 27  2016 sqlplus     -- sqlplus has read and execution permission have

-rw-r--r-- 1 oracle oracle   4369 Dec  5  2002 cpyus.msg -- .msg files all have read permission
-rw-r--r-- 1 oracle oracle 137350 Apr 20  2009 sp2us.msg
-rw-r--r-- 1 oracle oracle  20123 Sep 30  2009 sp1us.msg
-rw-r--r-- 1 oracle oracle  12288 Jul 17  2013 sp1us.msb
-rw-r--r-- 1 oracle oracle  34816 Jul 17  2013 sp2us.msb
-rw-r--r-- 1 oracle oracle   4096 Jul 17  2013 cpyus.msb

cron job is running with user ibrahim and set by user itself. * * * * * sh /home/ibrahim/Test.sh

Hi,

Now i have changed the entry in Cron like below,But still i am not getting the output.

          • /usr/bin/sh /home/ibrahim/Test.sh

Your ORACLE_HOME definition in the above full script is commented out. You may also need to set ORACLE_SID and ORACLE_BASE to whatever you have defined as well.

Hi,

Can you please check the code and let me know anything needs to be done.

Still i am facing the same.

Hi Jensen,

I have un commented the the # in ORACLE_HOME path in script. But still i am not getting the data.

Can you please explain ORACLE_SID and ORACLE_BASE how can i do?

#!/bin/bash
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
Cred=`cat < /home/ibrahim/Login`
RETVAL=` $Cred <<EOF
SET LINESIZE 30000
SET PAGESIZE 50000
SET feedback off
select * from emp;
EXIT;
EOF`
echo "$RETVAL" >> /tmp/Crontest

Cron entry:

* * * * * /usr/bin/sh /home/ibrahim/Test.sh

Hi Jensen,

Please check above code and let me know what needs to be change more.

Still i am facing the same issue now the requirement is more.

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)

Hi Dusan Baljevic,

Please find the my env result ~~~~~

XDG_SESSION_ID=164176 HOSTNAME=XXXXXXX TERM=vt220 SHELL=/bin/bash HISTSIZE=1000 TMPDIR=/var/tmp ORACLE_PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ibrahim/.local/bin:/home/ibrahim/bin:/home/ibrahim/.local/bin:/home/ibrahim/bin:/oracle/base/11.1.0/db/client/bin:/usr/ccs/bin:/usr/bin:/bin:.:/oracle/base/11.1.0/db/client/bin SSH_CLIENT=10.100.6.44 64047 22 OLDPWD=/tmp SSH_TTY=/dev/pts/7 http_proxy=http://10.100.1.201:9090 USER=ibrahim LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:.tar=01;31:.tgz=01;31:.arc=01;31:.arj=01;31:.taz=01;31:.lha=01;31:.lz4=01;31:.lzh=01;31:.lzma=01;31:.tlz=01;31:.txz=01;31:.tzo=01;31:.t7z=01;31:.zip=01;31:.z=01;31:.Z=01;31:.dz=01;31:.gz=01;31:.lrz=01;31:.lz=01;31:.lzo=01;31:.xz=01;31:.bz2=01;31:.bz=01;31:.tbz=01;31:.tbz2=01;31:.tz=01;31:.deb=01;31:.rpm=01;31:.jar=01;31:.war=01;31:.ear=01;31:.sar=01;31:.rar=01;31:.alz=01;31:.ace=01;31:.zoo=01;31:.cpio=01;31:.7z=01;31:.rz=01;31:.cab=01;31:.jpg=01;35:.jpeg=01;35:.gif=01;35:.bmp=01;35:.pbm=01;35:.pgm=01;35:.ppm=01;35:.tga=01;35:.xbm=01;35:.xpm=01;35:.tif=01;35:.tiff=01;35:.png=01;35:.svg=01;35:.svgz=01;35:.mng=01;35:.pcx=01;35:.mov=01;35:.mpg=01;35:.mpeg=01;35:.m2v=01;35:.mkv=01;35:.webm=01;35:.ogm=01;35:.mp4=01;35:.m4v=01;35:.mp4v=01;35:.vob=01;35:.qt=01;35:.nuv=01;35:.wmv=01;35:.asf=01;35:.rm=01;35:.rmvb=01;35:.flc=01;35:.avi=01;35:.fli=01;35:.flv=01;35:.gl=01;35:.dl=01;35:.xcf=01;35:.xwd=01;35:.yuv=01;35:.cgm=01;35:.emf=01;35:.axv=01;35:.anx=01;35:.ogv=01;35:.ogx=01;35:.aac=01;36:.au=01;36:.flac=01;36:.mid=01;36:.midi=01;36:.mka=01;36:.mp3=01;36:.mpc=01;36:.ogg=01;36:.ra=01;36:.wav=01;36:.axa=01;36:.oga=01;36:.spx=01;36:*.xspf=01;36: ORACLE_BASE=/oracle/base MAIL=/var/spool/mail/ibrahim PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ibrahim/.local/bin:/home/ibrahim/bin:/home/ibrahim/.local/bin:/home/ibrahim/bin:/oracle/base/11.1.0/db/client/bin:/usr/ccs/bin:/usr/bin:/bin:.:/home/ibrahim/.local/bin:/home/ibrahim/bin PWD=/home/ibrahim LANG=en_US.UTF-8 ORACLE_TERM=vt220 https_proxy=https://10.100.1.201:9090 HISTCONTROL=ignoredups SHLVL=1 HOME=/home/ibrahim TMP=/tmp LOGNAME=fpsweb LESSOPEN=||/usr/bin/lesspipe.sh %s XDG_RUNTIME_DIR=/run/user/1000 ORACLE_HOME=/oracle/base/11.1.0/db/client PERSISTENT_DATA_PATH=/usr/local/share/ _=/usr/bin/env

Cronjob scheduled like below 
          • /usr/bin/bash /fispss/fpsweb/Scripts/Test.sh 2> /tmp/TestFile.txt 2>&1
---/tmp/TestFile.txt this output nothing is generated.
 Mail massge

To: ibrahim@XXXXXX Subject: Cron <fpsweb@mudcimwbp02> /usr/bin/bash /home/ibrahim/Test.sh 2> /tmp/TestFile.txt 2>&1 Content-Type: text/plain; charset=UTF-8 Auto-Submitted: auto-generated Precedence: bulk X-Cron-Env: <XDG_SESSION_ID=165659> X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/1000> X-Cron-Env: <LANG=en_US.UTF-8> X-Cron-Env: <SHELL=/bin/sh> X-Cron-Env: <HOME=/home/ibrahim> X-Cron-Env: <PATH=/usr/bin:/bin> X-Cron-Env: <LOGNAME=ibrahim> X-Cron-Env: <USER=ibrahim> Message-Id: 20180612012601.E9D5922A6D@mudcimwbp02.localdomain Date: Tue, 12 Jun 2018 06:56:01 +0530 (IST)

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 ~~~

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)

... and ensure that you have correct ORACLE_HOME for your sp1.msb.

Find where it exists:

$ locate sp1.msb

Regards,

Dusan Baljevic (amateur radio VK2COT)

Hi Dusan Baljevic,

locate command is there in my environment and no permissions to install the package.

Finally,

    Good News Script working and getting the output through cron job.
#!/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

Thank you so much Dusan Baljevic for solving a major problem for me.

Thanks to all who helped for solving this issue.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.