O/s commands from PL/SQL
hi all..
i want to execute operating system commands from PL/SQL..
particulary imp command....
i have used the f/wg approach but while i execute the imp command it does not responds me any output..
I have to kill the process from o/s.
prodn is my user
--AS SYS user
begin
dbms_java.grant_permission( 'PRODN',
'SYS:java.io.FilePermission',
'/oracle/product/9.2.0/bin/imp',
'execute' );
dbms_java.grant_permission
('PRODN',
'java.lang.RuntimePermission',
'writeFileDescriptor' );
end;
---------------------------------AS PRODN user
create or replace and compile
java source named "Util"
as
import java.io.*;
import java.lang.*;
public class Util extends Object
public static int RunThis(String args)
Runtime rt = Runtime.getRuntime();
int rc = -1;
try
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis = new BufferedInputStream(p.getInputStream(),
bufSize);
int len;
byte buffer[] = new byte[bufSize];
// Echo back what the program spit out
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
catch (Exception e)
e.printStackTrace();
rc = -1;
finally
return rc;
create or replace
function RUN_CMD(p_cmd in varchar2) return number
as
language java
name 'Util.RunThis(java.lang.String) return integer';
create or replace procedure RC(p_cmd in varchar2)
as
x number;
begin
x := run_cmd(p_cmd);
end;
i executed the command as follows in sql>
BEGIN
rc('/oracle/product/9.2.0/bin/imp tim/tim file=/home/oracle/vas4359.dmp log
=log1.log');
END;
it runs fine creates log1.log in / home/oracle but there is no output in
log1.log file.
I have to kill the process as follows after which sql> displays me :
"PL/SQL procedure successfully completed."
[oracle@msebdb oracle]$ ps aux|grep imp
oracle 20052 0.0 0.2 12732 4328 ? S 11:57 0:00 /oracle/product/
.2.0/bin/imp file=/home/oracle/vas4359.dmp log=log1.log
oracle 20102 0.0 0.0 3692 652 pts/2 S 12:03 0:00 grep imp
Note:There is no tim user.. i just want to get error in my log file..
my further steps are to read this log file and send email/notifications to users.
my main program is as f/ws:
/*imp and check log file*/
create or replace procedure abc
as
v_exists boolean;
v_exists_log boolean;
ex_open BOOLEAN;
flen NUMBER;
bsize NUMBER;
vInHandle_open utl_file.file_type;
vInHandle_open_log utl_file.file_type;
v_filelog utl_file.file_type;
v_fl utl_file.file_type;
v_pth varchar2(60);
v_fnm varchar2(60);
v_file_exists varchar2(60);
v_logline varchar2(30) ;
v_search varchar2(30) :='ORA-';
vNewLine VARCHAR2(32767);
cursor c_fname is
select full_pth ,f_name from upld_file_t where f_name in
(select f_name from tbl_file_nm where typ_file='D' AND DEL_FLG='N');
BEGIN
for v_sec in c_fname
loop
utl_file.fgetattr(LOCATION=>'ORALOAD',
FILENAME=>v_sec.f_name,
FEXISTS=>v_exists,
FILE_LENGTH=> flen,
BLOCK_SIZE=>bsize);
if v_exists
THEN
dbms_output.put_line('File Exists' || v_sec.f_name);
v_file_exists:=v_sec.f_name;
--dbms_output.put_line('v_file_exists ' || v_file_exists);
dbms_output.put_line('File Length: ' || TO_CHAR(flen));
dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
dbms_output.put_line(' before file open');
vInHandle_open := utl_file.fopen('ORALOAD',v_sec.f_name,'R',32767);
-- dbms_output.put_line(' after file open');
-- do whater with teh file data
v_fl:=utl_file.fopen('ORALOAD','v_sec.f_name','W');
dbms_output.put_line('v_sec.f_name');
IF utl_file.is_open(vInHandle_open)
THEN
/*if log file exists..*/
utl_file.fgetattr(LOCATION=>'ORALOAD',
FILENAME=>v_sec.f_name||'.log',
FEXISTS=>v_exists_log,
FILE_LENGTH=> flen,
BLOCK_SIZE=>bsize);
if v_exists_log
then
dbms_output.put_line('file' ||v_sec.f_name||'.log');
v_filelog :=utl_file.fopen('ORALOAD',v_sec.f_name||'.log','W',32767);
LOOP
BEGIN
UTL_FILE.PUT(v_filelog,'ORA-');
exit;
END;
END LOOP;
else
dbms_output.put_line('File not found' || v_sec.f_name);
END IF;
UTL_FILE.fclose(v_filelog);
dbms_output.put_line('Closed File ' || v_sec.f_name|| '.log');
END IF;
/*imp THE FILE and read log file*/
utl_file.fgetattr(LOCATION=>'ORALOAD',
FILENAME=>v_sec.f_name||'.log',
FEXISTS=>v_exists_log,
FILE_LENGTH=> flen,
BLOCK_SIZE=>bsize);
if v_exists_log
then
dbms_output.put_line('file' ||v_sec.f_name||'.log');
v_filelog :=utl_file.fopen('ORALOAD',v_sec.f_name||'.log','R',32767);
LOOP
BEGIN
UTL_FILE.get_line(v_filelog,v_logline);
if v_logline = v_search then
dbms_output.put_line(v_logline);
end if;
UTL_FILE.fclose(v_filelog);
dbms_output.put_line('Closed File ' || v_sec.f_name|| '.log');
exit;
END;
END LOOP;
else
dbms_output.put_line('File not found' || v_sec.f_name);
END IF;
END IF;
END LOOP;
--close the curosr .. check this
EXCEPTION
WHEN UTL_FILE.ACCESS_DENIED THEN
DBMS_OUTPUT.PUT_LINE('No Access!!!');
when others then
dbms_output.put_line('ERROR (open_file) => '||sqlcode);
dbms_output.put_line('MSG (open_file) => '||sqlerrm);
end;
Hi,
You might have better luck in the PL/SQL or Java forums. They are located here:
PL/SQL: PL/SQL
Java: http://forums.oracle.com/forums/forum.jsp?forum=99
Good luck,
Mark
Similar Messages
-
Run OS command from Pl/SQL
Is there any way to execute an OS command from pl/sql other than using dbms_pipe & pro*C combo. I have done this way before but it seems there is built-in oracle package which can do this in 8/8i.
Also HOST command in forms will execute the OS coomand from the clinet. Is there any way to execute the OS command on the server from the forms clinet?
thanks,
Ravi.
nullRavi,
Can you send me an example how one can
execute a this command from a PL/SQL
program 'sed 3q /etc/passwd'.
Please write to me at
[email protected]
thanks
Pramod -
Calling Operating System Commands from PL/SQL using java
Calling Operating System Commands from PL/SQL - The Java Way
wlth help of given thread link,
Calling OS Commands from Plsql
but i had user privilege problem
Declare
x Varchar2(2000);
Begin
x := OSCommand_Run('/tmp/sri/GROUP_ho.sh');
DBMS_OUTPUT.Put_Line(x);
End;
o/p;
can't exec: /tmp/sri/GROUP_ho.sh lacks user privilege
i done the grant privillage also
part
dbms_java.grant_permission('abcd', 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
dbms_java.grant_permission('abcd', 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
dbms_java.grant_permission('
abcd', 'SYS:java.io.FilePermission','<<ALL FILES>>', 'execute');
again
get
can't exec: /tmp/sri/GROUP_ho.sh lacks user privilege
same error;Process management at the OS level should prevent execution continuing in the calling code until the command has completed (either successfully or with error).
If the low level java code were to spawn child process threads then execution could continue, but I'm guessing the Java function your talking about doesn't do that and just calls the operating system to execute the command and waits for the returning code to come back. -
How to: execute operating system commands from PL/SQL
System: IBM pSeries AIX
Oracle: 11g Enterprise
Is there a quick way to execute operating system commands from PL/SQL?
Without creating a JAVA class....
I need to execute a Loader script.
I want to do this from within PL/SQL.
I need to to do 4 things.
Three require SQL scripts.
The fourth is SQL*Loader.
The usual way is with a UNIX script calling 3 SQL scripts and one Loader control file.
Is there way to do all this from within ONE PL/SQL SCRIPT ??
Just let me know.Is there a quick way to execute operating system commands from PL/SQL?
NO
Without creating a JAVA class....
I need to execute a Loader script.
I want to do this from within PL/SQL.
WHY ON EARTH, such action will create a new connection and become a resource hog?
I need to to do 4 things.
Three require SQL scripts.
The fourth is SQL*Loader.
The usual way is with a UNIX script calling 3 SQL scripts and one Loader control file.
Which is perfect for the purpose.
Is there way to do all this from within ONE PL/SQL SCRIPT ??
Just let me know.
NO, there is NOT. Nor should there be. Why create unstable ineffcient Mickey Mouse systems, glued together by hacking?
Sybrand Bakker
Senior Oracle DBA -
Running sqlldr command from PL/SQL Block
DECLARE
BEGIN
END;In SQL * plus we can run DOS commands using the following command
HOST DIR
HOST DIR/P
But When we can't run the HOST command in PL/SQL Block..
I have to Run sqlldr command from PL/SQL Block..
i tried as follows
DECLARE
BEGIN
EXECUTE IMMEDIATE ' host sqlldr control= bad= ';
END;
By
BalaNagaRaju -
Fire OMB Plus commands from PL/SQL
Hi all,
does anyone know if it is possible to fire OMB Plus commands from PL/SQL procedures?
I can imagine that there might be a PL/SQL package in the repository owner schema that accepts OMB Plus commands as strings...or something similar...
Thanks,
ElioHi Elio
I don't know about any existing package that could do that.
What I'd suggest is to build a stored procedure/function/package that could do that. Or even, execute OMB Scripts in the host from a pl/sql program.
Good luck.
Regards,
Marcos -
OS command from PL/SQL code
Is there any way of running on OS (Operating System) command from SQL or PL/SQL. i.e. calling an executable from a trigger.
Is there any way of running on OS (Operating System) command from >SQL or PL/SQL. i.e. calling an executable from a trigger. Ashis,
Here's an example of a simple piece of Java you can create using SQL*Plus:
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "RuntimeExample" AS public class RuntimeExample {
public static String runNotepad() {
String returnValue = null;
Runtime r = Runtime.getRuntime();
try {
Process p = r.exec("notepad.exe");
returnValue = p.toString();
} catch (Exception e) {
System.out.println("Exception calling Runtime.exec()->" + e);
/* You can get creative here and
1) Use p.waitFor() to block while the process is running,
2) Send the command to be executed as a parameter to the method, etc. */
return returnValue;
CREATE OR REPLACE FUNCTION runNotepad RETURN VARCHAR2 IS
LANGUAGE JAVA NAME 'RuntimeExample.runNotepad() return java.lang.String';
DECLARE
my_varchar2 VARCHAR2(64);
BEGIN
my_varchar2 := runnotepad;
DBMS_OUTPUT.PUT_LINE(my_varchar2);
END;
Hope this helps,
-Dan
http://www.compuware.com/products/numega/dbpartner/dbpordebug.htm
Debug Java in the Oracle Database -
Sending OS command from PL/SQL procedure
"How can I send a operating system comand from PL/SQL procedure?
I want to move , to copy , delete a file from a PL/sql procedure. i.e under unix send mv, cp or
rm command";
my e-mail is [email protected]take a look at
http://asktom.oracle.com/pls/ask/f?p=4950:8:881946
regards
Freek D'Hooge
"How can I send a operating system comand from PL/SQL procedure?
I want to move , to copy , delete a file from a PL/sql procedure. i.e under unix send mv, cp or
rm command";
my e-mail is [email protected] -
How to issue a SQL*Plus command from PL/SQL program?
Thanks.
You can't issue a SQL*PLus command from a PL/SQL program. If you can tell us more about what you are trying to do, perhaps we can offer a substitute.
-
Exec SQLPLUS command from PL/SQL Block
Good Morning:
How I can execute a SQLPLUS command (like SPOOL or DESCRIBE) from PL/SQL Block Procedure?:
DECLARE
BEGIN
ls_command = 'DESCRIBE '||ls_table_name;
EXECUTE SQLPLUS(ls_command);
END;
Thanks a lot for any idea.That's correct.
However, in the case of the given example we can use DBMS_DESCRIBE package to get table descriptions. And we can use UTL_FILE to spool PL/SQL stuff to a file.
Cheers, APC -
How to run OS commands from PL/SQL???
Hi
Is there any way to run OS commands(Windows Platform) from within PL/SQL?
ThanksAPC,
I am working on Discoverer, having lots of BAs & Workbooks created & stored in Database. Now if i want to move all the workbooks from one place to another (like from development to test or production) i can use Command line interface of Disco to do this. But i don't see any option there to export all the workbooks, so i thought of writing a pl/sql to get all the workbook names from the EUL then fire export Command for each record(although not a good practice but its a one time work). This is the place where i need to run OS command from within PL/SQL. Although it doesn't seem possible now(as you all said) without external procedure.
Lastly, you are right that "Oracle make databases not operating systems" but my first impression/comment about oracle is:
"Oracle is far Bigger, Powerful, Complex, Vast and Interesting System than any other..." so i believe it can do/capable doing anything :-)
thx Ashutosh,
Host is definitely an option but i can't run it from pl/sql(that i am looking for).. -
Hi,
We use JAVA source in Oracle to execute some unix commands directly from within stored procedures in Oracle.
Our Java source looks like this :
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "HOST" AS
import java.lang.*;
import java.io.*;
public class Host
public static void executeCommand (String command, String etype) throws IOException
String[] wFullCommand = {"C:\\winnt\\system32\\cmd.exe", "/y", "/c", command};
String[] uFullCommand = {"/bin/sh", "-c", command};
if (etype.toUpperCase().equals("W"))
Runtime.getRuntime().exec(wFullCommand);
else if(etype.toUpperCase().equals("U+"))
Runtime.getRuntime().exec(uFullCommand);
else if(etype.toUpperCase().equals("U"))
Runtime.getRuntime().exec(command);
CREATE OR REPLACE PROCEDURE Host_Command (p_command IN VARCHAR2, p_etype IN VARCHAR2)
AS LANGUAGE JAVA
NAME 'Host.executeCommand (java.lang.String, java.lang.String)';
We call one unix shell script and discover that some UNIX commands from this shell script are executed and others not. A cp & touch command don't give problems whilst cat & unix2dos commands don't function.
Can anybody help me on this subject?
Thanks !
KrisThanks for your reactions. Got it to work with the unix scripts, the problem was a syntax error in the script itself.
However I don't get it to function when I try to run a unix command directly. Normally the parameter U+ should decide to start it in a shell for scripts and otherwise it should run the command directly. (whithout the /bin/sh in front of it).
I already adapted my source to try to execute the command in a new shell but without success :
String[] uFullCommand = {"/bin/sh", "-c", "\'cp /export/home/caluwaek/test/bestand2 /export/home/caluwaek/test/bestand8\'"};
I tried to put the whole command in one string, in 3 strings, to just call the cp command without bothering the shell. Nothing seems to work.
In unix both the following work :
cp /export/home/caluwaek/test/bestand2 /export/home/caluwaek/test/bestand8
and
/bin/sh -c 'cp /export/home/caluwaek/test/bestand2 /export/home/caluwaek/test/bestand8'
Thanks for your help!
Kris -
Calling Operating system command from PL/SQL programs
Hi
Is there any way we call the unix shell script from PL/SQL procedure/functionsIn 10g, it is possible.
BEGIN
dbms_scheduler.create_job(job_name => 'myjob',
job_type => 'executable',
job_action => '/app/oracle/x.sh',
enabled => TRUE,
auto_drop => TRUE);
END;
SQL> exec dbms_scheduler.run_job('myjob');
Documentation:
http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sched.htm#sthref6596
-aijaz -
Calling UNIX command from PL/SQL Procedure
Is there a way to call a UNIX command (exa. ls) from within a PL/SQL stored procedure? Looking through the various packages, I didn't see anything that would give me this ability.
I also looked through all the previous questions and nothing looked encouraging.
Thanks in advance,
RussRuss,
I thought I'd read a similar question awhile ago and did a search on the word host in this forum and found the following from July 9th that might help you:
http://technet.oracle.com:89/ubb/Forum88/HTML/001611.html
In that post I think the link listed by Barbara Boehmer in has changed to:
]http://asktom.oracle.com/pls/ask/f?p=4950:8:24579::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:16212348050,{host}
It looks like they give solutions for Oracle 7, 8 and 8i.
Good site Barbara, I've added it to my bookmarks.
Hope this helps. -
Running linux commands from pl/sql
Hi, I'm using a java class to run os commands but it doesn't work, i think it's permissions but i cant understand why.
the java class is the following:
import java.io.*;
import java.lang.*;
public class RunCmd extends Object{
public static int RunThis(String args){
Runtime rt = Runtime.getRuntime();
int rc = -1;
try{
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis =
new BufferedInputStream(p.getInputStream(), bufSize);
int len;
byte buffer[] = new byte[bufSize];
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
catch (Exception e)
e.printStackTrace();
rc = -1;
finally
return rc;
the pl/sql function i created:
function run_cmd(p_cmd in varchar2) return number as language java name 'RunCmd.RunThis(java.lang.String) return oracle.sql.NUMBER';
i gave the following permissions:
begin
dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'execute');
dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'write');
dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'read');
dbms_java.grant_permission(user, 'SYS:java.io.FilePermission', '/home/mails/*', 'delete');
dbms_java.grant_permission(user, 'SYS:java.lang.RuntimePermission', '*', 'readFileDescriptor');
dbms_java.grant_permission(user, 'SYS:java.lang.RuntimePermission', '*', 'writeFileDescriptor');
Dbms_Java.grant_Permission(user, 'SYS:java.lang.RuntimePermission', 'readFileDescriptor', '');
Dbms_Java.grant_Permission(user, 'SYS:java.lang.RuntimePermission', 'writeFileDescriptor', '');
dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'execute');
dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'write');
dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'read');
dbms_java.grant_permission(user, 'java.io.FilePermission', '/home/mails/*', 'delete');
dbms_java.grant_permission(user, 'java.lang.RuntimePermission', '*', 'readFileDescriptor');
dbms_java.grant_permission(user, 'java.lang.RuntimePermission', '*', 'writeFileDescriptor' );
Dbms_Java.grant_Permission(user, 'java.lang.RuntimePermission', 'readFileDescriptor', '');
Dbms_Java.grant_Permission(user, 'java.lang.RuntimePermission', 'writeFileDescriptor', '');
commit;
end;
The command i'm trying to execute is:
select run_cmd('/bin/sh -c "ls -ltr /home/mails> /home/mails/teste.txt"') from dual;
i gave chmod 777 /home/mails and i already run this command directly in console mode and works just fine, so i dont think it's permissions related to the user oracle.
when i run
select run_cmd('/bin/sh -c "ls -ltr /home/mails> /home/mails/teste.txt"') from dual;
it returns 2
if i run
select run_cmd('/bin/sh -c ls -ltr /home/mails> /home/mails/teste.txt') from dual;
it returns 127
if i run just
select run_cmd('ls -ltr /home/mails> /home/mails/teste.txt') from dual;
it returns -1
if anyone as any clue please, i'm trying to solve this problem for 2 days and i don't have any clue for the solution...when i remove the /bin/sh -c it returns -1
i think that it runs the command because there is a diference in the return value from a valid command a an invalid, for example:
if i run a valid command
select k_cs_controlo.run_cmd('/bin/sh -c "ls -ltr /home/mails > /home/mails/teste.txt"') from dual;
it retuns 2
if i run an invalid command(removed the " between the ls command)
select k_cs_controlo.run_cmd('/bin/sh -c ls -ltr /home/mails > /home/mails/teste.txt') from dual
it returns 127
thanks for the replies Sven
Maybe you are looking for
-
HT4259 With extended network, which device do I connect to?
I set up my Time Capsule as my base station and extended the network using an Airport Express. My question is, when I am logged onto the base station, will my computer automatically select whichever signal is stronger, the time capsule or the airport
-
When I click on the Firefox icon on my desktop the browser appears but a pop-up also appears with the above statements. It also suggests that "this may be phoney and someone may be trying to impersonate the site and you shouldn't continue." What do I
-
Moving iTunes to an external drive
Can anyone walk me through how to relocate my iTunes software from my full C drive to an external drive? I can't update the iTunes software or my iPhone, because my C drive is so full.
-
Group Text with Android Doesn't Work
Hi- I had been using an iPhone for a few years, and decided to switch over to Android. It took some time to get my phone number disassociated from iMessage/iCloud, but I was eventually able to get that done. Since iOS8 came out, though, I'm starting
-
ATI HD5770 and 2008 Mac Pro - Only DVI Port works
I went through 4 ATI Radeon HD 2600 video cards since 2008. Enough is enough. I bought a ATI HD5770 and i hope this is the end of it. However, I have two monitors and I am unable to use the second monitor. The main monitor is hooked into the DVIPor