在Oracle運行作業系統命令

來源:互聯網
上載者:User

在Oracle 8i中,往往會出現要在預存程序中運行作業系統命令的情況.一般來說,利用Oracle Enterprise Manager設定作業時可以達到這個目的.但是由於OEM在設定作業缺乏靈活性,設定的作業的參數是固定的.在實際應用當中往往需要在SQL語句當中運行需要隨時運行作業系統命令.Oracle 8i沒有直接運行OS命令的語句,我們可以利用DBMS_PIPE程式包實現這一要求.
DBMS_PIPE通過建立管道,可以讓至少兩個進程進行通訊.Oracle的管道與作業系統的管道在概念上有相同的地方,但是在實現機制不同. 下面介紹實現具體步驟:
1 建立一個程式包,姑且起名叫DAEMON,SQL語句如下:
/*建立daemon程式包*/
CREATE OR REPLACE PACKAGE BODY daemon AS
/*execute_system是實現運行os命令的函數*/
FUNCTION execute_system(command VARCHAR2,
timeout NUMBER DEFAULT 10)
RETURN NUMBER IS
status NUMBER;
result VARCHAR2(20);
command_code NUMBER;
pipe_name VARCHAR2(30);
BEGIN
pipe_name := DBMS_PIPE.UNIQUE_SESSION_NAME;
DBMS_PIPE.PACK_MESSAGE('SYSTEM');
DBMS_PIPE.PACK_MESSAGE(pipe_name);
DBMS_PIPE.PACK_MESSAGE(command);
/*向daemon管道發送表示命令的字元*/
status := DBMS_PIPE.SEND_MESSAGE('daemon', timeout);
IF status <> 0 THEN
RAISE_APPLICATION_ERROR(-20010,
'Execute_system: Error while sending. Status = ' || status);
END IF;
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;
/*擷取返回結果*/
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是讓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;
通過Sql*Plus運行以上語句,將為目前使用者建立daemon程式包.
2 建立在OS上啟動並執行守護進程,監聽由上面的daemon程式包發來的要求執行os命令的語句.以下Pro*C的代碼,必須由pro*c先進行先行編譯.
#include
#include
EXEC SQL INCLUDE SQLCA;
EXEC SQL BEGIN DECLARE SECTION;
char *uid = "scott/tiger";/*在這個地方改為你自己訪問的使用者,密碼,服務名*/
int status;
VARCHAR command[20];
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;
EXEC SQL WHENEVER SQLERROR CONTINUE;
sqlglm(msg_buffer, &buffer_size, &msg_length);
printf("Daemon error while connecting:\n");
printf("%.*s\n", msg_length, msg_buffer);
printf("Daemon quitting.\n");
exit(1);
}
void
sql_error()
{
char msg_buffer[512];
int msg_length;
int buffer_size = 512;
EXEC SQL WHENEVER SQLERROR CONTINUE;
sqlglm(msg_buffer, &buffer_size, &msg_length);
printf("Daemon error while executing:\n");
printf("%.*s\n", msg_length, msg_buffer);
printf("Daemon continuing.\n");
}
main()
{
EXEC SQL WHENEVER SQLERROR DO connect_error();
EXEC SQL CONNECT :uid;
printf("Daemon connected.\n");
EXEC SQL WHENEVER SQLERROR DO sql_error();
printf("Daemon waiting...\n");
while (1) {
EXEC SQL EXECUTE
BEGIN
/*接收deamon發來的字元*/
:status := DBMS_PIPE.RECEIVE_MESSAGE('daemon');
IF :status = 0 THEN
/*取出字元*/
DBMS_PIPE.UNPACK_MESSAGE(:command);
END IF;
END;
END-EXEC;
IF (status == 0)
{
command.arr[command.len] = '\0';
/*如果是stop,該進程就退出*/
IF (!strcmp((char *) command.arr, "STOP"))
{
printf("Daemon exiting.\n");
break;
}
ELSE IF (!strcmp((char *) command.arr, "SYSTEM"))
{
EXEC SQL EXECUTE
BEGIN
DBMS_PIPE.UNPACK_MESSAGE(:return_name);
DBMS_PIPE.UNPACK_MESSAGE(:value);
END;
END-EXEC;
value.arr[value.len] = '\0';
printf("Will execute system command '%s'\n", value.arr);
/*運行os命令*/
status = system(value.arr);
EXEC SQL EXECUTE
BEGIN
DBMS_PIPE.PACK_MESSAGE('done');
DBMS_PIPE.PACK_MESSAGE(:status);
:status := DBMS_PIPE.SEND_MESSAGE(:return_name);
END;
END-EXEC;
IF (status)
{
printf
("Daemon error while responding to system command.");
printf(" status: %d\n", status);
}
}
ELSE
{
printf
("Daemon error: invalid command '%s' received.\n",
command.arr);
}
}
ELSE
{
printf("Daemon error while waiting for signal.");
printf(" status = %d\n", status);
}
}
EXEC SQL COMMIT WORK RELEASE;
exit(0);
}
以上代碼起名為daemon.pc,用proc先行編譯:
proc iname=daemon.pc userid=使用者名稱/密碼@服務名 sqlcheck=semantics
得到daemon.c,在用c進行編譯,注意在NT上要把orasql8.lib加上,否則編譯通過,串連沒法通過.
3 在伺服器上運行daemon.exe
4 在sqlplus運行測試語句:
SQL> variable rv number
SQL> execute :rv := DAEMON.EXECUTE_SYSTEM('ls -la');
PL/SQL 過程已成功完成。
SQL> execute :rv := DAEMON.EXECUTE_SYSTEM('dir');
PL/SQL 過程已成功完成。
SQL>
DBMS_PIPE的用法見oracle的文檔.

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.