SoFunction
Updated on 2025-04-07

How to run OS commands directly (Part 1) on page 2/2

status := DBMS_PIPE.RECEIVE_MESSAGE(pipe_name, timeout);
IF status <> 0 THEN 
RAISE_APPLICATION_ERROR(-20011, 
'Execute_system: Error while receiving. 
Status = ' || status);
END IF;
/*Get back result*/
DBMS_PIPE.UNPACK_MESSAGE(result);
IF result <> 'done' THEN 
RAISE_APPLICATION_ERROR(-20012, 
'Execute_system: Done not received.');
END IF;

DBMS_PIPE.UNPACK_MESSAGE(command_code);
DBMS_OUTPUT.PUT_LINE('System command executed. result = ' || 
command_code);
RETURN command_code;
END execute_system;
/*stop is to stop daemon*/
PROCEDURE stop(timeout NUMBER DEFAULT 10) IS 
status NUMBER;
BEGIN 
DBMS_PIPE.PACK_MESSAGE('STOP');
status := DBMS_PIPE.SEND_MESSAGE('daemon', timeout);
IF status <> 0 THEN 
RAISE_APPLICATION_ERROR(-20030, 
'stop: error while sending. status = ' || status);
END IF;
END stop;
END daemon;

Running the above statement through Sql*Plus will create a daemon package for the current user.

2. Create a daemon running on the OS and listen to statements sent by the above daemon package requiring the execution of the os command. The following Pro*C code must be precompiled by pro*c.

#include 
#include 

EXEC SQL INCLUDE SQLCA;

EXEC SQL BEGIN DECLARE SECTION;
char *uid = "scott/tiger";/*In this place, change it to the user, password, service name you accessed by yourself*/
int status;
VARCHAR command;
VARCHAR value[2000];
VARCHAR return_name[30];
EXEC SQL END DECLARE SECTION;

void 
connect_error() 

char msg_buffer[512];
int msg_length;
int buffer_size = 512;


Previous page12Read the full text