LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem in executing script through crontab (https://www.linuxquestions.org/questions/linux-newbie-8/problem-in-executing-script-through-crontab-4175533141/)

azheruddin 02-05-2015 04:56 AM

Problem in executing script through crontab
 
Hi,
I have written one script which is connecting to the the daabase and generating one CSV, it is running fine when i ran it manually though it is throwing any warning but CSV is genearting and working fine but same script when i have configured in crontab not working and giving error, kindly help.

script contents:

#!/bin/bash
sqlplus -s /nolog << EOF
CONNECT plmc/plmb@whb
@csvgenerator.sql
end;
/
EOF
-----------------------------------
Script running successfully as below, though it is giving below message but running fine

$ sh test.sh
SP2-0734: unknown command beginning "sqlplus ct..." - rest of line ignored.

Session altered.
CSV geneartaed

Same Script giving error while triggering by crontab as below

$ sqlplus: not found

chrism01 02-05-2015 05:01 AM

The env for cron jobs is entirely separate and usually much more minimal than a user's env.
In short, use absolute full paths to all cmds and files used or source a file that creates the correct env at the top of the shell file.

Incidentally, I'd definitely try to fix those errors; it may be important later ...

azheruddin 02-05-2015 10:27 AM

Update
 
Hi Chris,
Thanks, all the given paths are absoulute in the files for all the files.

issue seems to be that script is not able to connect to database by the sqlplus -s /nolog << EOF while running through crontab .

but when running manually script is connecting to database and output is coming with some message "SP2-0734: unknown command beginning "sqlplus ct..." - rest of line ignored."

Kindly assist.

suicidaleggroll 02-05-2015 10:44 AM

Quote:

Originally Posted by azheruddin (Post 5312558)
Thanks, all the given paths are absoulute in the files for all the files.

Then what's this?
Quote:

Originally Posted by azheruddin (Post 5312558)
sqlplus -s /nolog << EOF

"sqlplus" is not absolute, it relies on the directory containing the "sqlplus" command being in the PATH environment variable, which is different for the user than it is in cron, as chrism01 was saying.

As he said earlier:
Quote:

use absolute full paths to all cmds and files used
If you don't know the full path to "sqlplus", then in a terminal use "which sqlplus", and put the result in your script.

azheruddin 02-05-2015 07:37 PM

Update
 
Hi,

Now I have Set the sqlplus absoulte path as below
/export/home/oracle/app/oracle/product/11.2.0/client_1/bin/sqlplus -s /nolog << EOF


New Error occured as below, kindly assist.

Error 6 initializing SQL*Plus
SP2-0667: Message file sp1<lang>.msb not found
SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory

suicidaleggroll 02-05-2015 07:51 PM

Pretty self-explanatory...it needs the ORACLE_HOME environment variable to be set. See what your user has it set to and add the appropriate setting in the script.

azheruddin 02-05-2015 09:47 PM

Hi,
it seems already set oracle_home environment

$ env
ORACLE_HOME=/export/home/oracle/app/oracle/product/11.2.0/client_1

In script i am connecting as below.


/export/home/oracle/app/oracle/product/11.2.0/client_1/bin/sqlplus -s /nolog << EOF

what i need to set in script?

azheruddin 02-06-2015 02:13 AM

After setting in the environment varibale..

below error appears when it triggers via crontab.


SP2-0310: unable to open file "crewpair_data.sql"
SP2-0042: unknown command "end" - rest of line ignored.
SP2-0103: Nothing in SQL buffer to run.

manual script run working fine. Please assist.

chrism01 02-06-2015 02:20 AM

As I said and suicidaleggroll re-iterated, use FULL path to aLL CMDS & ALL files.
Alternately, use a wrapper to ensure you cd to the correct dir before you start.

Seriously:

1. write shell wrapper and add
Code:

#!/bin/bash
set -xv

at the top.

Also ensure you log all output eg
Code:

... myjob.sh >/tmp/myjob.log 2>&1

azheruddin 02-06-2015 10:57 AM

Sorry chris, but iam using everywhere absoulte path only , please refer below a small snippet.

crontab :

Code:

06 00 * * * ksh  ". ./.profile; /data2/users/cmw/PKG12/test_data.sh" > /tmp/testoutput.txt 2>&1
Script code:

Code:

#!/bin/ksh
/export/home/oracle/app/oracle/product/11.2.0/client_1/bin/sqlplus -s /nolog << EOF
CONNECT user/user_1234@CBDTEST
@extract_data.sql [checked absolute path for too]
end;
/
EOF


Error:

Code:

ncmsas:/tmp $ cat testoutput.txt
SP2-0310: unable to open file "extract_data.sql"
SP2-0042: unknown command "end" - rest of line ignored.
SP2-0103: Nothing in SQL buffer to run.

By the above error it seems script is not able to connect to Database , please assist me what exact changes needs to be done in Script..

azheruddin 02-06-2015 10:23 PM

anyone assist here?

suicidaleggroll 02-06-2015 10:36 PM

Quote:

Originally Posted by azheruddin (Post 5313097)
Code:

06 00 * * * ksh  ". ./.profile; /data2/users/cmw/PKG12/test_data.sh" > /tmp/testoutput.txt 2>&1

What is ./.profile?

./ means "here", are you sure that .profile is in the directory where the cronjob is being executed? That should probably be "$HOME/.profile".


is extract_data.sql a local or remote file? I'm not too familiar with sql.

azheruddin 02-06-2015 11:26 PM

Hi,

Yes, ./.profile is the same which other cron jobs are executing successfully but those are connecting to database.

extract_data.sql is the local file,and resides at same location where script is there.

suicidaleggroll 02-07-2015 10:18 AM

Quote:

Originally Posted by azheruddin (Post 5313396)
extract_data.sql is the local file

Then you need to use its full path, and has been said countless times in this thread.

Use absolute paths for all commands and files.

azheruddin 02-07-2015 11:05 PM

Update
 
Hi,
Changes done as suggested and getting below..

Script code :

Code:

/export/home/oracle/app/oracle/product/11.2.0/client_1/bin/sqlplus -s /nolog << EOF
CONNECT user/pass123@mhbtest
/data2/users/mhdb/SRAS_interface/@extract_data.sql
end;
/
EOF


Error:
Code:

SP2-0734: unknown command beginning "/data2/use..." - rest of line ignored.
SP2-0042: unknown command "end" - rest of line ignored.
SP2-0103: Nothing in SQL buffer to run.



All times are GMT -5. The time now is 08:42 AM.