Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 
 

UNIX/Linux commands from inside Oracle

Oracle Tips by Burleson Consulting

 

Question:  How can I execute a UNIX or Linux command from inside a PL/SQL stored procedure?

 

Answer:  Oracle has many features such as utl_file that write to the operating system environment, and you have choices.  Dr. Hall has several working examples of the invocation of operating system commands from inside PL/SQL in his book "PL/SQL Tuning". There are two common approaches to invoking UNIX/UNIX commands from inside PL/SQL:

 

  • UNIX Commands via external procedures (the extproc procedure)

  • Linux commands from Oracle with dbms_pipe

 

Invoking UNIX commands from Oracle via external procedures

 

Jeff Hunter has these instructions on executing OS commands using Oracle external procedures:

 

*****************************************************

 

External procedure source code

shell.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


void mailx(char *to, char *subject, char *message) {

  int num;
  char command[50000];

  strcpy(command, "echo \"");
  strcat(command, message);
  strcat(command, "\" | mailx -s \"");
  strcat(command, subject);
  strcat(command, "\" ");
  strcat(command, to);

  num = system(command);

}

void sh(char *command) {

  int num;

  num = system(command);

}
Listing 3 - Sample external procedure code

Issue the following commands to compile the code and generate the shared object in Solaris:

  gcc -G -c shell.c
  ld -r -o shell.so shell.o
  chmod 775 shell.so
You can also use the demo_rdbms.mk makefile to build shared libraries for use in external procedures. Using this method insulates you from any Operating System specific dependencies (e.g., which flags to use for ld).

For example:

 

  • In 8.0.X,
      $ make -f demo_rdbms.mk extproc_nocallback \
      SHARED_LIBNAME=shell.so OBJS=shell.o
  • In 8.1.X,
      $ make -f demo_rdbms.mk extproc_no_context \
      SHARED_LIBNAME=shell.so OBJS=shell.o

 

Library Definition

 

  CREATE LIBRARY shell_lib is '/u01/app/oracle/c/shell.so';
  /
Note: The directory in quotes is the current of location of the library that was created in the steps above.

 

PL/SQL Wrapper Procedure

 

Create PL/SQL Wrapper Procedures
CREATE OR REPLACE PROCEDURE shell(command IN char)
  AS EXTERNAL
     NAME "sh"
     LIBRARY shell_lib
     LANGUAGE C
     PARAMETERS (command string);
  /

  CREATE OR REPLACE PROCEDURE mailx(send_to IN char, subject IN char, message IN char)
  AS EXTERNAL
     NAME "mailx"
     LIBRARY shell_lib
     LANGUAGE C
     PARAMETERS (send_to string, subject string, message string);
  /
Listing 4 - Sample PL/SQL Wrapper Procedure

 

Execution

 

  SQL> exec shell('ls');

  cli.trc           exe_prba.sql      prueba.mk         shell.c.old
  core              listener.old      prueba.o          shell.o
  dec2bin.c         listener.ora      prueba.so         shell.so
  dec2bin.c.old     nena.lst          prueba.sql        shell.sql
  dec2bin.o         p.sql             sal.1             shell.sql.old
  dec2bin.so        pepito.lst        sal.2             sqlnet.log
  dec2bin.sql       prb               sal.3             tnsnames.ora
  dec2bin.sql.old   prb.c             salida.lst        tnsnames.ora.old
  envoltorio.sql    prueba.c          shell.c           uno.sql

  PL/SQL procedure successfully completed.


  SQL> exec mailx('Jeffrey.Hunter@marconi.com', 'EXTPROC Test', 'This is a test');

  PL/SQL procedure successfully completed.

The output produced by the executed command is not viewable in general since it is directed to the controlling terminal for the extproc process. The extproc process inherits its controlling terminal from the listener, which in turn inherits its terminal from the shell used to start the listener. If this shell is no longer visible, the output is never displayed.

To see the output returned from the system command, redirect the output to a file and then view/process the output file. This can be done simply on UNIX platforms by appending "> myoutput.txt" to the command being executed. Standard error can be redirected similarly.

The executed commands only see the directory pointed by the TNS_ADMIN environment variable defined in the server, therefore, when specifying a file, be sure to include the path of its desired location.

 

 


 

 

??
 
  
 
 
 

 
 
 

Oracle training Excel
 
Oracle performance tuning software 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright ? 1996 -  2014

All rights reserved by Burleson

Oracle ? is the registered trademark of Oracle Corporation.