Calling Shell script from pl/sql procedure [message #626668] |
Thu, 30 October 2014 07:53 |
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');
|
|
|
|
|