DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: Execute a UNIX command from pl/sql

  1. #1
    Join Date
    Feb 2001
    Posts
    34

    Question

    Folks!

    8.1.6 on Solaris 2.6

    Is there a way to execute a UNIX command from pl/sql? My goal is to kick off a sqlldr session from a procedure.

    Thanks in advance for your insight!

    Joe B.

  2. #2
    Join Date
    Apr 2001
    Posts
    219
    Here is something I got from the net.

    This tip comes from Dave Sienknecht, Programmer for Genesys Software Consulting in Sunnyvale, CA.

    Calling Unix commands from PL/SQL isn't a new idea, but this technique allows the user or program to execute any given OS command, view the printed output, and capture a success/fail argument.

    Directions:

    Compile the java program.
    Load it to the DB using LOADJAVA.
    Create a function call spec to call the program.
    Turn serveroutput and java output ON.

    Function Call Spec:
    ----------------------------------------
    CREATE OR REPLACE FUNCTION shellcmd (v_cmd IN VARCHAR2) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'oscmd.mainrun(java.lang.String[]) return int';
    /


    Java Source:
    ----------------------------------------
    import java.io.*;
    import java.util.*;

    public class oscmd{
    static public int runCommand(String cmd)
    throws IOException {

    int vSubResult;
    vSubResult = 0;

    // set up list to capture command output lines
    ArrayList list = new ArrayList();

    // start command running
    Process proc = Runtime.getRuntime().exec(cmd);

    // get command's output stream and
    // put a buffered reader input stream on it
    InputStream istr = proc.getInputStream();
    BufferedReader br =
    new BufferedReader(new InputStreamReader(istr));

    // read output lines from command
    String str;
    while ((str = br.readLine()) != null)
    list.add(str);

    // wait for command to terminate
    try {
    proc.waitFor();
    }
    catch (InterruptedException e) {
    System.err.println("process was interrupted");
    vSubResult = 1;
    }

    // check its exit value
    if (proc.exitValue() != 0) {
    System.err.println("exit value was non-zero");
    vSubResult = 1;
    }

    // close stream
    br.close();

    // read all result strings into variable outlist
    String outlist1[] = (String[])list.toArray(new String[0]);

    // display the output
    for (int i = 0; i < outlist1.length; i++)
    System.out.println(outlist1[i]);

    return vSubResult;
    }

    public static int mainrun(String args[]) throws IOException {
    try {
    int vResult;

    // run the given command
    vResult = runCommand(args[0]);

    // return 0 on success, 1 on failure
    return vResult;
    }
    catch (IOException e) {
    System.err.println(e);
    return 1;
    }
    }
    }


  3. #3
    Join Date
    Feb 2001
    Posts
    34
    Thanks Zaggy, I'll give that a shot!

    Joe B.

  4. #4
    Join Date
    Mar 2001
    Location
    Ireland/Dublin
    Posts
    688
    1) you have to write C++ programm which execute any UNIX commands

    http://www.dbasupport.com/forums/sho...threadid=12299

    2) write PL/SQL procedure which call external function from C++

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width