Call shell commands in pl/SQL

Source: Internet
Author: User
Tags call shell

Use the dbms_pipe package and create a daemon running on the OS

I think this method is complicated. I still need to use Pro * C. I didn't try it. Here is an example.

Use Java's getruntime(cmd.exe C

Java uses a lot of such advantages.

1) write a simple Java program executecmd. Java2) compile and generate executecmd. Class

Import java. Lang. runtime;
Import java. Lang. process;
Import java. Io. ioexception;
Import java. Lang. interruptedexception;

Class executecmd ...{

Public static void main (string ARGs [])... {

System. Out. println ("start executing ");

Try ...{

/** // * Execute the command using the runtime object and get
Process which controls this command */
PROCESS p = runtime.getruntime(cmd.exe C (ARGs [0]);

/** // * Use the following code to wait for the process to finish
And check the return code from the process */
Try ...{

P. waitfor ();

/** // * Handle exceptions for waitfor ()*/
} Catch (InterruptedException intexc )...{
System. out. println ("Interrupted Exception on waitFor:" + intexc. getMessage ());
}

System. out. println ("Return code from process:" + p. exitValue ());
System. out. println ("Done executing ");

/** // * Handle the exceptions for exec ()*/
} Catch (IOException e )...{

System. Out. println ("Io exception from Exec:" + E. getmessage ());
E. printstacktrace ();

}
}
}

 

Javac executecmd. Java

3) load to Oracle

$ Loadjava-user system/manager ExecuteCmd. class

4) generate a java stored procedure

Create or replace procedure executecmd (S1 varchar2)
As language Java
Name 'executecmd. Main (Java. Lang. String []) ';
/

 

Test:

SQL> set serveroutput on
SQL> call dbms_java.set_output (2000 );
SQL> EXEC executecmd ('/bin/touch/home/oracle/a.txt ');
Start executing
Return code from process: 0
Done executing
PL/SQL procedure successfully completed.

SQL> host
$ Ls/home/oracle
A.txt

The execution is successful, but there are still some problems. For example, the Environment Variable EXEC executecmd ('/bin/touch$ Home/A.txt ') execution fails. If the path is absolute or relative, the execution user (here the system user) must be granted the relevant permissions. Therefore, I think we should first write a shell executable script for what we want to do, and then call the above Code to save some trouble.

Executable jobs of Oracle

There is no research on DBMS_SCHEDULER. There is an example on the Forum, but I didn't try it out. If I have time to study it again, I should be able.

Exec dbms_scheduler.create_job (job_name => 'test13', job_type => 'executable', job_action => '/tmp/test1.sh ');
Exec dbms_scheduler.run_job (job_name => 'test13 ');
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.