rem -----------------------------------------------------------------------
rem Filename: oscmd.sql
rem Purpose: Execute operating system commands from PL/SQL
rem Notes: Specify full paths to commands, for example,
rem specify /usr/bin/ps instead of ps.
rem Date: 09-Apr-2005
rem Author: Frank Naude, Oracle FAQ
rem -----------------------------------------------------------------------
rem -----------------------------------------------------------------------
rem Grant Java Access to user SCOTT
rem -----------------------------------------------------------------------
conn / as sysdba
EXEC dbms_java.grant_permission('SCOTT', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
EXEC dbms_java.grant_permission('SCOTT', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
EXEC dbms_java.grant_permission('SCOTT', 'SYS:java.io.FilePermission', '/bin/sh', 'execute');
-- Other read ,write or execute permission may be requried
rem -----------------------------------------------------------------------
rem Create Java class to execute OS commands...
rem -----------------------------------------------------------------------
conn scott/tiger
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Host" AS
import java.io.*;
public class Host {
public static void executeCommand(String command) {
try {
String[] finalCommand;
if (System.getProperty("os.name").toLowerCase().indexOf("windows") != -1) {
finalCommand = new String[4];
finalCommand[0] = "C:\\winnt\\system32\\cmd.exe";
finalCommand[1] = "/y";
finalCommand[2] = "/c";
finalCommand[3] = command;
} else { // Linux or Unix System
finalCommand = new String[3];
finalCommand[0] = "/bin/sh";
finalCommand[1] = "-c";
finalCommand[2] = command;
}
// Execute the command...
final Process pr = Runtime.getRuntime().exec(finalCommand);
// Capture output from STDOUT...
BufferedReader br_in = null;
try {
br_in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String buff = null;
while ((buff = br_in.readLine()) != null) {
System.out.println("stdout: " + buff);
try {Thread.sleep(100); } catch(Exception e) {}
}
br_in.close();
} catch (IOException ioe) {
System.out.println("Error printing process output.");
ioe.printStackTrace();
} finally {
try {
br_in.close();
} catch (Exception ex) {}
}
// Capture output from STDERR...
BufferedReader br_err = null;
try {
br_err = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String buff = null;
while ((buff = br_err.readLine()) != null) {
System.out.println("stderr: " + buff);
try {Thread.sleep(100); } catch(Exception e) {}
}
br_err.close();
} catch (IOException ioe) {
System.out.println("Error printing execution errors.");
ioe.printStackTrace();
} finally {
try {
br_err.close();
} catch (Exception ex) {}
}
}
catch (Exception ex) {
System.out.println(ex.getLocalizedMessage());
}
}
};
/
show errors
rem -----------------------------------------------------------------------
rem Publish the Java call to PL/SQL...
rem -----------------------------------------------------------------------
CREATE OR REPLACE PROCEDURE host (p_command IN VARCHAR2)
AS LANGUAGE JAVA
NAME 'Host.executeCommand (java.lang.String)';
/
show errors
rem -----------------------------------------------------------------------
rem Let's test it...
rem -----------------------------------------------------------------------
CALL DBMS_JAVA.SET_OUTPUT(1000000);
SET SERVEROUTPUT ON SIZE 1000000
exec host('/usr/bin/ls');
Execute operating system commands from PL/SQL
thanks, good set up info, easy to follow.
i already tested it's ok as this step
example : i create user " SHARE_SERVICE " to be owner of java source and do as the following
1. login as sys or system
2. create procedure grant java permission as GRANT_JAVA_PERMISSION
download script "cr_grant_java_permission.zip" from blog it knowledge Indy | Oracle Tips
after create procedure then run execute to user "SHARE_SERVICE" as
SQL> exec grant_java_permission('SHARE_SERVICE');
3. login as "SHARE_SERVICE" user
and create java source and packages via download script "os_command_java.zip" from blog it knowledge Indy | Oracle Tips
4. after completed create java source, package, then testing as (current user SHARE_SERVICE)
SQL> select os_command.exec_clob(’/bin/date’) from dual;
OS_COMMAND.EXEC_CLOB(’/BIN/DATE’)
———————————————-
Sun Jul 12 00:07:03 ICT 2009
for function EXEC_CLOB , it'll return output but if you don't want output return then used EXEC funcation as
SQL> select os_command.exec(’/bin/date’) from dual; OS_COMMAND.EXEC(’/BIN/DATE’)
—————————-
0
note : return "0" (zero) mean run successful. else failed.
5. if you would like to share public you can download script as grant_public.zip from
blog it knowledge Indy | Oracle Tips
This is nice! Thanks for that.
I still have an issue. I'm using Windows XP as OS. What do I need to do when command is a batch file located in a different server?
I tried mapping units and didn't work, then I tried using \\ServerName\foldername\* but it didn't work either.
I tried going to the blog ot knowledge Indy | Oracle Tips and could not find the os_command_java.zip download. Do you know how to find it?
I don't have a google account but my email is dkreimer@kreimercomputing.com
Thanks in advance.
I tried going to the blog ot knowledge Indy | Oracle Tips and could not find the os_command_java.zip download. Do you know how to find it?
I don't have a google account but my email is dkreimer@kreimercomputing.com
Thanks in advance.
I tried going to the blog ot knowledge Indy | Oracle Tips and could not find the os_command_java.zip download. Do you know how to find it?
I don't have a google account but my email is dkreimer@kreimercomputing.com
Thanks in advance.