Home » SQL & PL/SQL » SQL & PL/SQL » Calling Shell script from pl/sql procedure (11g R2, Linux(unix))
Calling Shell script from pl/sql procedure [message #626668] Thu, 30 October 2014 07:53 Go to next message
srinivas.k2005
Messages: 404
Registered: August 2006
Senior Member
I am trying call unix shell script from oracle procedure using the Java code.

Shellscript contains only a touch command as below and it runs with oracle user which has all the privileges on the directory.
--test.sh
#!/bin/sh
cd /app/cpr_data/scripts/
touch test.txt

when i run the procedure it is executing the shell script but not creating the file in the directory.

Java code:

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED GFL_COMMON_DATA."OSCommand" as
import java.io.*;
import java.lang.*;
public class OSCommand{


public static String Run(String Command){

Runtime rt = Runtime.getRuntime();
int rc = -1;

try{

Process p = rt.exec( Command );
int bufSize = 32000;
int len = 0;
byte buffer[] = new byte[bufSize];
String s = null;

BufferedInputStream bis = new BufferedInputStream( p.getInputStream(), bufSize );
len = bis.read( buffer, 0, bufSize );

rc = p.waitFor();

if ( len != -1 ){
s = new String( buffer, 0, len );
return( s );
}

return( rc+"" );
}

catch (Exception e){
e.printStackTrace();
return( "-1\ncommand[" + Command + "]\n" + e.getMessage() );
}

}
}

/


PRIVILEGES GIVEN DBMS_JAVA:

EXEC Dbms_Java.Grant_Permission('GFL_COMMON_DATA', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '*');

EXEC dbms_java.grant_permission('GFL_COMMON_DATA','SYS:java.lang.RuntimePermission','readFileDescriptor','*');

EXEC dbms_java.grant_permission('GFL_COMMON_DATA', 'SYS:java.io.FilePermission','<<ALL FILES>>', 'read ,write, execute, delete');
EXEC dbms_java.grant_permission('GFL_COMMON_DATA', 'SYS:java.io.FilePermission','/app/cpr_data/scripts/test.sh', 'read ,write, execute, delete');

EXEC Dbms_Java.Grant_Permission('GFL_COMMON_DATA', 'SYS:java.net.SocketPermission', '*', 'connect,resolve');



---Procedure to call the Unix command:

CREATE OR REPLACE procedure GFL_COMMON_DATA.OSexec( cCommand IN string ) is
-- function: OS EXEC
-- descr: PL/SQL wrapper for the Java OSCOMMAND stored proc
--
language JAVA
name 'OSCommand.Run(java.lang.String)';
/


-- executing the procedure

exec OSexec('/app/cpr_data/scripts/test.sh');





Re: Calling Shell script from pl/sql procedure [message #626671 is a reply to message #626668] Thu, 30 October 2014 07:58 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>when i run the procedure it is executing the shell script but not creating the file in the directory.

I don't believe you.

Do you know that DBMS_SCHEDULER can invoke OS script?

P.S.
We are not here to debug your Java code.
Re: Calling Shell script from pl/sql procedure [message #626678 is a reply to message #626668] Thu, 30 October 2014 11:39 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

/app/cpr_data/scripts/test.sh is not a direct executable program.
Try "/bin/sh -c /app/cpr_data/scripts/test.sh" (or wherever is your sh or change to bash or whatever is your shell).

Previous Topic: Time Difference Between two Time
Next Topic: Transforming rows to colums
Goto Forum:
  


Current Time: Thu Apr 18 14:00:48 CDT 2024