Running OS Command From SQL
Hellow,
Can Any Anyone tell me how to run an Operating System Command From SQL ???
Or From Developer 6i ??
Regards
Tariq
Use Host command
Similar Messages
-
Hi All!!
Which package can help me to run OS command from SQL script?
Thanks.If you are using SQL*Plus to execute the script, you can use the SQL*Plus command HOST. If you are trying to execute an operating system command from a PL/SQL block (i.e. a stored procedure), you would need to use an external procedure or a Java stored procedure that uses Java's shell functionality.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
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 -
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).. -
Running ls command from Java stroed procedure no output
Hi ,
I am trying to run ls command from java stored procedure in oracle
Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("output of the command run:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
from java stored procedure in oracle.
i get output of println statments but it does not go into while loop to print from stdInput.
Result of running Java stored procedure is -
output of the command run:
Call completed.
when i run the program on client side it works fine.
Has anybody tried this from java stroed procedure.
Thanks,
JagJag,
Actually, the question of whether it works for me seems to depend on the version of the OS (or Oracle). On RedHat Linux (Oracle 8.1.6) it didn't work at all, but on Solaris (Oracle 9.0.2) it did. Here's the output from that run:
SQL> /
output of the command run:
init.ora
initDBPart9i.DBPSun01.ora
initdw.ora
lkDBPART9I
orapw
orapwDBPart9i
spfileDBPart9i.ora
Done
PL/SQL procedure successfully completed.
But, I did need to change a line of your code to this:
Process p = Runtime.getRuntime().exec("/usr/bin/ls");
your original was:
Process p = Runtime.getRuntime().exec("ls");
You might consider, if possible, use of some of the Java File classes instead of ls, as this might make things more predictable for you. There were some examples in oramag.com a few months ago, but they were pretty simple (you might not need them).
Hope this helps,
-Dan
http://www.compuware.com/products/devpartner/db/oracle_debug.htm
Debug PL/SQL and Java in the Oracle Database -
dear all,
is it possible to run os commands with sql commands as follows :
SQL> ALTER system kill session '1764,60569'; ! kill -9 12151
2 /
ALTER system kill session '1764,60569'; ! kill -9 12151
ERROR at line 1:
ORA-00911: invalid character
kai
os version : solaris 5.10.. db : 10.2.0.4.0Kais,
What exact error are you getting running this script and what's OS (is it linux RH?? If you can post errors on running this script, that will be helpful. I tested on both cygwin (my desktop) and liunx box but you can change as needed) and to kill process from script you can use
echo `kill -9 pid`
Hope this helps
#!/bin/bash
typeset username=$1
function readSqlstmt {
typeset stmt=$1
echo "
set feedback off
set verify off
set heading off
set pagesize 0
whenever sqlerror exit 1
whenever oserror exit 2
$stmt;
exit
" | sqlplus -S '/as sysdba >> test.log
function killpid {
killpidsql=killpid.sql
sqlplus -s '/as sysdba' << ENDOFSQL >> test.log
whenever sqlerror exit 1
whenever oserror exit 2
SET pagesize 0
SET verify off
SET feedback off
SPOOL ${killpidsql}
SELECT 'ALTER system kill session ''' || s.sid || ',' || s.serial# || ''';'
FROM v\$session s, v\$process p
WHERE s.paddr = p.addr AND s.status = 'INACTIVE';
SPOOL OFF
@${killpidsql}
ENDOFSQL
killpid
readSqlstmt "SELECT p.spid FROM v\$session s, v\$process p WHERE s.paddr = p.addr AND s.status = 'INACTIVE'" | while read u
do
#echo "kill -9" $u >> test.log
echo "Killing inactive oracle process..."
echo `kill -9 $u`
done -
Running ssh command from java and then answering password prompt
Hi,
I have a situation that has not solved yet. I am running ssh command from unix terminal without any problem, and then i enter password.
For example :
[oracle@fuata]:/export/home/oracle> ssh -N [email protected] -L 9901:127.0.0.1:9999
Password:
It is working. I have question that how can i perform this in java? I am thinking that i can run ssh command by using Runtime Class, it is ok. But how can i answer the password? I am a bit confused. Is there any example looks like this?
Thanks for responses.futi wrote:
Thanx. Firstly i insisted to do this without jsch but actually this is harder than jsch. I edit some of code pieces PortForwardingL.java and could run it. It works problem-free. Could you say why you "insisted" on this approach. It can't be for speed+ since jsch is very fast. It can't be for portability+ since jsch is portable but the use of Runtime.exec() requires the installation of ssh software. It can't be because of limitations+ since jsch is a fully featured library. It can't be for security+ since jsch is secure. It can't be for ease of use+ since jsch is much easier to use than ssh with Runtime.exec(). Unless it's a licensing issue, it can't be for commercial+ reasons since jsch is free. The only reason I can think of why one would "insisted" on this approach is if it is for some college project. -
Dear Guru's,
I have Script of my all the procedure.
After all procedure gets execute, As a output it create .csv files.
This .csv files i manually copy past on one of my directory.
For avoiding this manual copy past i want to use xcopy command from sql itself.
Please guide me how do i do this.
Regards,
Chanchal wankhade.CHACHA wrote:
I have Script of my all the procedure.
After all procedure gets execute, As a output it create .csv files.
This .csv files i manually copy past on one of my directory.
For avoiding this manual copy past i want to use xcopy command from sql itself.
Please guide me how do i do this.Do not like the sound of the architecture you are using for this. Scripts? Scripts for Oracle should be limited to installing s/w.
Writing any kind of Oracle application using scripts, is questionable ito design, robustness, flexibility, security and so on.
Wanting to use console commands from a SQL cursor? That sounds even more broken and wrong. If you want to do o/s calls, it should be done carefully and securely from PL/SQL code.
And PL/SQL code should be inside the database - not outside in some script.
As for copying files. Realise that SQL and PL/SQL code executes on the server inside an Oracle server process. This does not have access to your client's file system and directories. -
Running Unix Command from WEB-APPLICATION
Hi all,
I want to run unix command from a java-based web application. the basic code part is this ---
public class RunCommand
public String runIt()
String s = null, returnString = "";
Process p=null;
try
Runtime rt = Runtime.getRuntime();
p = rt.exec("sh testPOC.ksh");
p.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
returnString += "Here is the standard output of the command:<br>";
while ((s = stdInput.readLine()) != null) {
returnString += s;
// read any errors from the attempted command
returnString += "Here is the standard error of the command (if any): <br>";
while ((s = stdError.readLine()) != null) {
returnString += s;
catch (IOException e)
returnString += "exception happened - here's what I know: ";
returnString += "error-> " + e.getMessage();
catch(Exception e)
returnString += "exception happened - here's what I know: ";
returnString += "error-> " + e.getMessage();
return returnString;
}this class is kept as an inner class. The control comes to its outer class, from servlet, from which the runit() is called. but the exception is occuring at line of p=rt.exec(.....). it tells "<command name> : not found transaction completed" [got this using getMessage() method].
i am unable to show(and see, too) the stacktrace, because i don't have access to that test environment and its log. i can't run this in local because its windows one.
now can anyone tell me, where is the problem. is there any limitation in web application server/container? this was successful when i used command prompt writing a .java file. Please help me. Thanks in advance...Friends, i've got, where the problem is.
when we run a class file directly from a command prompt, we get an environment with that shell window. but for a servlet application running these kind of commands from a class creates kind of child processes. each and every command is executed as a child process of jvm and don't get those environment. we have 'PATH' variable in the environment. when a command (say, 'dir' or 'sh' or 'ls', etc.) is executed, the shell first search for that executable file (i.e. dir / sh / ls) in the given paths in the variable 'PATH'. this is not available for the child commands of jvm. hence the basic commands are searched in the current directory of the jvm and they are failed.
i solved the problem giving full path of the commands. like :
p = rt.exec("/bin/sh runningScript.ksh") -
Is it possible to run host command from SAP environment? How do you run?
Hi
Is it possible to run host command from SAP environment? How do you run?
Thank YouHello Subhash
You will more details in the following thread:
Re: How to define command for SXPG_COMMAND_EXECUTE
Regards
Uwe -
How to run O/S command from SQL * Plus.
Hi,
Can i run O/S commands, say copy or move, from SQL * Plus prompt? If yes, what is the command.
Thanks.hi,
or you can try the following
slq > !
and then your command
rgds
Alan -
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 -
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 -
Running OS command from PL/SQL
Hi All,
Is there any way of running a Operating System command thru the PL/SQL Procedure. I am using oracle on the Unix Platform. Do help me.
Thanks In advance,
Nitin JainAs one who did it, here's what I recommend.
declare
cmdstr varchar2(200); -- this is to compose the OS command to run
begin
-- then you should have something like this
cmdstr:=...;
cmdstr:=cmdstr| |...;
host(cmdstr); -- this actually runs the command held in the cmdstr varchar
end;
Beware: you must be certain which OS you have to deal with, for some procs may be run on UNIX machines as well as on WINDOZE! -
hi I am using this code which needs to copy file
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RunSystemCommand {
public static void main(String args[]) {
String s = null;
// system command to run
String cmd = "copy a.txt b.txt";
// set the working directory for the OS command processor
File workDir = new File("c:
temp");
try {
Process p = Runtime.getRuntime().exec(cmd, null, workDir);
int i = p.waitFor();
if (i == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdErr.readLine()) != null) {
System.out.println(s);
catch (Exception e) {
System.out.println(e);
it doesnt work,the error is
java.io.IOException: CreateProcess: copy a.txt b.txt error=2
thx,ShaiIf you are using SQL*Plus to execute the script, you can use the SQL*Plus command HOST. If you are trying to execute an operating system command from a PL/SQL block (i.e. a stored procedure), you would need to use an external procedure or a Java stored procedure that uses Java's shell functionality.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
Maybe you are looking for
-
Parent-Child Hierarchy in Informatica
Can anyone please tell me the solution for the below scenario in Informatica. I have seen this scenario in folkstalk.com Take a look at the following tree structure diagram. From the tree structure, you can easily derive the parent-child relationshi
-
I've created a dynamic pdf form in Livecycle ES 8.2 which has been working just fine up until a recent revision. As a quick overview, the form has fillable fields, checkboxes which control the visibility of certain fields (through JS), and signature
-
Query to find the size of trigger?
query to find the size of trigger?
-
Pushbuttons on application toolbar in selection screen
Hi, I have created 2 pushbuttons in the application tool bar of selection screen using pf-status.But the other icons in the standard toolbar were grayed out because of this.can anyone tell me why this is happening.
-
Is anyone else having issues with their FabricSkin keyboard not connecting in ios7?
Since upgrading to iOS7 I've not been able to connect to my FabricSkin keyboard. Is anyone else having issues with Bluetooth?