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

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

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

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 








UNIX/Linux commands from inside Oracle

Oracle Database Tips by Donald Burleson


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


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.o
  chmod 775
You can also use the 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 extproc_nocallback \ OBJS=shell.o
  • In 8.1.X,
      $ make -f extproc_no_context \ OBJS=shell.o


Library Definition


  CREATE LIBRARY shell_lib is '/u01/app/oracle/c/';
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
     NAME "sh"
     LIBRARY shell_lib
     PARAMETERS (command string);

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




  SQL> exec shell('ls');

  cli.trc           exe_prba.sql         shell.c.old
  core              listener.old      prueba.o          shell.o
  dec2bin.c         listener.ora
  dec2bin.c.old     nena.lst          prueba.sql        shell.sql
  dec2bin.o         p.sql             sal.1             shell.sql.old        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('', '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 at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


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 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.