Link to home
Start Free TrialLog in
Avatar of joekeri
joekeriFlag for United States of America

asked on

Can I run a UNIX script from within an Oracle PL/SQL procedure?

Can I run a UNIX script from within an Oracle PL/SQL procedure?

If yes, then please show me an example..
Avatar of newfiepicks
newfiepicks

Yes you would do it using external procedure to run host commands. A step by step example is attached
extproc.doc.docx
Avatar of joekeri

ASKER

Hello newfiepicks...

I am unable to read the attachment you sent. I tried al my avaible word processign programs and nothing opens it up. Can you send it as a TXT file instead?
attached a RTF
ASKER CERTIFIED SOLUTION
Avatar of newfiepicks
newfiepicks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joekeri

ASKER

excellent! thanks for your help..
Trying to keep formatting. dashes are being removed hopefully this code snippet will keep formatting.
To do this, I use a simple oracle external procedure. The example Im presenting below has been tested and used on Oracle 10gR2 and RedHat Linux AS3.
Heres how it works:
1) First create a directory on your unix box to hold the C program we will write, its makefile, and the compiled .so file. On my server I created the directory using the command:
mkdir p /u01/app/usfapp/oracle_external_procs/oracle_host
 
 
2) Change to the directory you just created, all files created below will be created in this directory. Make sure you are the oracle user and that the oracle user has write privileges to the directory you just created.
 
 
3) Create a file named oracle_host.c. The contents of this file will be:
#include 
 
int RunCmd(char *cmd)
{
        return(system(cmd));
}
 
 
4) Create the file named makefile. The contents of this file will be:
oracle_host: oracle_host.o
        gcc -shared -o oracle_host.so oracle_host.o
Note that the character before the gcc start to the second line MUST be a tab. It must not be anything else.
 
 
5) Now run the command make
The output on my server looks like:
[/u01/app/usfapp/oracle_external_procs/oracle_host]
banner@usfbannerte [TRNG] > make
gcc -shared -o oracle_host.so oracle_host.o
 
 
6) When you reach this step, you should have a file in the current directory named oracle_host.so. 
 
Congratulations, the hard part is done!
 
 
7) Now we need to tell oracle about the external procedure. Login to oracle through SQL Plus, or your favorite Oracle tool such as TOAD or SQL Navigator. Login as the system user, or as a user who as the appropriate privileges to create libraries.
 
 
8) Create the library in oracle that will tell the system the location of our newly compiled oracle_host.so file. Run the following command in your SQL Plus session.
CREATE LIBRARY usf_host_lib as '/u01/app/usfapp/oracle_external_procs/oracle_host/oracle_host.so';
The user must have the CREATE LIBRARY privilege who will be creating the library. In my server, as the system user I run the following as the system user:
GRANT CREATE LIBRARY TO tima;
 
 
9) Create the PL/SQL function that we will use to call the library we created and execute our host commands.
CREATE OR REPLACE FUNCTION USF_RUN_HOST_CMD (p_Cmd IN VARCHAR2) RETURN PLS_INTEGER AS
EXTERNAL
LIBRARY USF_HOST_LIB
NAME "RunCmd"
PARAMETERS (p_Cmd STRING);
 
 
10) Now, we need to do a little server configuration and ensure that the listener.ora and tnsnames.ora files on our database server are setup so that our external procedures can be called. First off the listener.ora file needs to have appropriate entries. In my listener.ora file I added the sections:
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ANY")
    )
And the section:
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
My complete listener.ora file looks like:
SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ANY")
    )
    (SID_DESC =
      (GLOBAL_DBNAME = TRNG.db.timarcher.com)
      (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
      (SID_NAME = TRNG)
      (ENVS = "EXTPROC_DLLS=ANY")
    )
  )
 
LISTENER =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = db.timarcher.com)(PORT = 16969))
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
  )
 
 
11) Now we change our tnsnames.ora file. I added the following to the bottom of my tnsnames.ora file:
EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )
My complete tnsnames.ora file now looks like:
TRNG =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = db.timarcher.com)(PORT = 16969))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = TRNG. db.timarcher.com)
    )
  )
EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )
 
 
12) Now that the server configuration files have been setup, we need to restart our listener. From the unix shell prompt, as the oracle user we run the following commands:
lsnrctl stop
lsnrctl start
 
 
13) Finally, you can call our new function to execute a host command. An example of a PL/SQL script to echo the word test to the file /tmp/tim.txt is:
declare
  nRetVal NUMBER;
begin
  nRetVal := USF_RUN_HOST_CMD('echo test >> /tmp/tim.txt');
  dbms_output.put_line('RetValue:'||nRetVal);
end;
The function USF_RUN_HOST_CMD will pass back the unix return code from the process you run. For most processes a 0 means success, and a non zero value means it failed.

Open in new window

Found the link where I got the original information. Should be all nice and formatted here:

http://timarcher.com/node/9