You are on page 1of 7

Unix for Oracle Database Administrators

Robert Wylie, DBA, SSG VLC, O/o t e Acco!ntant General"A#$%, Ra&ast an, 'ai(!r

The Article is a quick reference describing, how to create complex one-line Unix commands in addition to some most often used Unix commands used by Oracle database administrators.

B!ildin) Unix Commands


One of the most confounding things for the Unix neophyte is being confronted with a complex Unix command. The cryptic nature of Unix is such that e en the most seasoned Unix professional may ha e trouble deciphering such a command. !egarding the terms commands and scripts, you should note that any command may become a script if it is encapsulated into a file for execution. "ence, find . # print can be a command if executed from the prompt, or a script if placed into a file.

B!ildin) a Com(lex Unix Command from Scratc


A complex, one-line script is really composed of se eral simpler commands connected by the pipe operator $%&. 'e will start by how to build a one-line script to kill all the Oracle processes on your ser er. Then we will see how to build oneline script to find files that contain a specific text string. A scri(t to *ill all Oracle (rocesses This is a common Unix script used by Oracle ()As when a database is locked up, and *er er +anager cannot be used to stop the database in more ,gentle, fashion. To begin, the Unix kill command is used to kill a process. The basic format of the kill command is as follows.ill #/ PID1 PID2 PID3 PIDn The trick is to be able to identify and kill only the Oracle processes. That0s done by stringing se eral commands together. The resulting one-line script looks like thisUnix for Oracle Database Administrators

"+%

ps #ef%grep ,ora12%grep # grep%grep 3O!A4561*7(% awk 89print 3:;0%xargs kill #/ 'e 8ll walk through the process of building the command. To begin, we want to get a list of acti e processes on the ser er. 'e can do that using the following commandps #ef 7f we execute ps ef on our ser er, we0 ll see a long list of processes - both for Oracle and for many other things. "owe er, we want to limit your output to only those processes that are related to the Oracle (atabase. The grep command can be used to do this. Oracle background process names always begin with ,ora12, so piping the output of ps ef through grep ora_ will remo e all but the Oracle background processes. <or example,(s -ef .)re( /ora01 oracle oracle oracle oracle oracle oracle oracle oracle oracle =:>== =:CD/ =F:>: =G/DF =>:>/ :>/> =>G>G =>G>F =>G>= = :>:>: = = = = = = = > > > > > > > > > (ec >? =:-=>-EE (ec >? (ec >? (ec >? (ec >? (ec >? (ec >? (ec >? @ >->> @ @ @ @ @ @ @ ora1dbwr1AAB grep ora1 ora1smon1AAB ora1arch1AAB ora1pmon1AAB ora1reco1AAB ora1lgwr1AAB ora1dbwr1T6*T ora1lgwr1T6*T

7n the abo e output as we can see it includes the process that0s running grep command. Bipe this output through grep v grep to remo e the grep command, so you don0t kill your own process. The #v option makes grep work in a way that0s opposite its usual manner. 'hereas grep finds and includes strings, grep v excludes strings. 7n this next example, we0 ll see that the grep line is now missing from the output,(s -ef .)re( /ora01.)re( -2 )re( oracle oracle oracle oracle oracle oracle oracle oracle =:>== =F:>: =G/DF =>:>/ :>/> =>G>G =>G>F =>G>= = = = = = = = = > > > > > > > > (ec >? (ec >? (ec >? (ec >? (ec >? (ec >? (ec >? (ec >? @ @ @ @ @ @ @ @ ora1dbwr1AAB ora1smon1AAB ora1arch1AAB ora1pmon1AAB ora1reco1AAB ora1lgwr1AAB ora1dbwr1T6*T ora1lgwr1T6*T
"3%

Unix for Oracle Database Administrators

Hext, we should filter out all processes except those for the current ORACLE_ ID. That way we delete the background processes only for that one instance instead of for all instances $if there are multiple database instance running&. (o that by grepping for the ID name,(s -ef .)re( /ora01.)re( -2 )re(.)re( 4ORACL$0S5D oracle oracle oracle oracle oracle oracle =:>== =F:>: =G/DF =>:>/ :>/> =>G>G = = = = = = > > > > > > (ec >? (ec >? (ec >? (ec >? (ec >? (ec >? @ @ @ @ @ @ ora1dbwr1AAB ora1smon1AAB ora1arch1AAB ora1pmon1AAB ora1reco1AAB ora1lgwr1AAB

How that we ha e an accurate list of processes that you want to kill, you can use the a!k command to get the process 7( $B7(& for each of these processes. The B7( is in the second column, so we will use the a!k "#prin$ %2&' command to display only that column,(s -ef .)re( /ora01.)re( -2 )re(.)re( 4ORACL$0S5D. a6* 78(rint 439: =:>== =F:>: =G/DF =>:>/ :>/> =>G>G How we ha e a list of process 7d numbers for the Oracle background processes. <or the last step, we use the (args command to pipe the list of B7(s to the kill command. <or example,(s -ef .)re( /ora01.)re( -2 )re(.)re( 4ORACL$0S5D. a6* 78(rint 439:.xar)s *ill -; How that we0 e created this compound command, we can assign it to a Unix alias or we can put it in a file and make it a shell script so that we can execute it with a single short command. <ote= Hot all shells support aliases. <or example, if we are using the )ourne shell we will not be able to use aliases.

Unix for Oracle Database Administrators

">%

The following command assigns the new compound command to an alias named kill_ora)lealias *ill0oracle ? /(s -ef .)re( /ora01.)re( -2 )re(.)re( 4ORACL$0S5D.a6* 78(rint 439:.xar)s *ill -;1 )y placing the command to create the alias in your *profile file, we0ll ha e it a ailable e ery time you sign on to Unix. )y using an alias, you encapsulate the command without the burden of placing the command into a script file. How, entering the alias kill_ora)le at the command prompt will cause your command to run, which will kill all Oracle background processes for the instance to which %ORACLE_ ID points. A scri(t to find all files containin) a s(ecific strin) 7n Unix, it is not easy to find files that contain specific strings. How we will explore a way to quickly build a command that will allow us to find a file that contains a particular character string. Using commands such as (args, we can quickly generate Unix scripts to perform many useful tasks. *uppose that we ha e created a script to create database T6*T. Unfortunately, we ha e completely forgotten the name and location of the script file, and we need a Unix command to locate it. The example here demonstrates how you can le erage the (args command to quickly create a complex command that searches for our lost file. 'e will begin by writing a command that will display all filenames on the ser er. This syntax is quite simple in Unix, as the fin+ command can be used to return a list of e ery file on the ser er starting from your current directory,find @ -(rint IhomeIoracleIwylieIsqlnet.log IhomeIoracleIwylieIabc.sql IhomeIoracleIwylieItablespace.sql IhomeIoracleIwylieIcreate=.sql IhomeIoracleIwylieIexport.dmp IhomeIoracleIwylieIcreate:.sql IhomeIoracleIwylieIcreateF.sql IhomeIoracleIwylieIa.txt

Unix for Oracle Database Administrators

"A%

'e now ha e a complete list of all the Unix files under our current directory. The next step is to pipe this list of filenames to the grep command to search for files containing the string 4!6AT61()1T6*T. )ecause the grep command accepts a filename as an argument, you can use (args to execute a grep command to search each file for the string we needfind . #print%xargs grep #i create1db1test The #i option tells grep to ignore case. 'e can execute this new command at the Unix prompt, and we0ll see that it quickly finds the file we are seeking,find @ -(rint.xar)s )re( -i create0db0test This ability to take a basic Unix command and pipe the output into another command is a fundamental principle of Unix shell programming for Oracle.

Unix Ser2er $n2ironment


"ere are some handy Unix commands that will make it easier for use to na igate in our Unix en ironment. <irstly we will see at commands that can be automatically executed when we sign on to Unix as the Oracle user. There is a special file in our home directory in which we can place Unix commands that we want automatically executed when we sign on to the system. 7f we use .orn shell, this file is named *profile. if we use 4 shell, it will be called .)s,r). 'e will also see how to create a standard Unix prompt, wrap *J5 in Unix script, and write a utility to quickly change all files in a directory. Set a Standard Unix Brom(t Blacing the following code snippet in our . profile file will gi e you a Unix prompt that identifies our current ser er name, database name, and working directory. This can help pre ent you from accidentally running a command against the wrong database. Hote that 7 ha e my prompt go to a new line after displaying the information, so that 7 ha e a full C/ characters in which to type my Unix commands. KLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL K *tandard Unix Brompt KLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL B*=M2 NhostnameNLO39O!A4561*7(;-O39B'(; P2

Unix for Oracle Database Administrators

"C%

"ere is what the prompt looks like after we ha e executed the P 1 command shown in the pre ious example. Hote now the prompt changes when you change directories. agQ=LAAB-IhomeIoracle ,(6d IhomeIoracle agQ=LAAB-IhomeIoracle ,cd / ome3/dm(/treas!ry agQ=LAAB-Ihome:IdmpItreasury P Create Usef!l Unix Aliases for Oracle "ere we will see how we can place a list of helpful Unix aliases in the *profile file of a Unix Oracle User. An alias is a Unix shortcut whereby we can define a short name to use in place of long Unix command. <or example, we can create a shortcut called ,log2 that would execute the Unix )+ $change directory& command to take us to the Unix directory where our alert log is locatedalias log M 8cd 3()AI3O!A4561*7(Ibdump0 The following example shows how aliases such as these can be used in place of typing a long command3log 3pwd IhomeIoracleIappIoracleIadminIAABIbdump 3 Any alias can be remo ed easily with the Unix -nalias command. <or example, to remo e the log alias, we would enter the command -nalias log. Blace a SDLEBl!s Scri(t in a Unix S ell Wra((er Ho ice to Unix often find it con enient to execute *J5 commands directly from the Unix prompt, without ha ing to enter *J5LBlus each time. The following script shows how to create a Unix shell wrapper for any set of *J5LBlus commands. The Unix script in this example is named r-n_s.l*ks,, and it in okes *J5LBlus to execute a *6564T statement followed by the *J5LBlus script contained in the file /,o0e/ora)le/a1)*s.lUnix for Oracle Database Administrators

"F%

4cat r!n0sGl@*s KRIbinIksh K<irst, we must set the en ironment . . . . . O!A4561*7(Mmysid export O!A4561*7( O!A4561"O+6MONcat IetcIoratab%grep S3OA!4561*7( cut #f: #d0-0N export O!A4561"O+6 BAT"M3O!A4561"O+6Ibin-3BAT" export BAT" 3O!A4561"O+6IbinIsqlplus systemIpasswdTTR *6564T L <!O+ O3databaseU VIhomeIoracleIabc.sql exit R 'e can also execute a script directly from the command line, pro ided we ha e set ORACLE_2O3E and ORACLE_ ID in our Unix en ironment. <or example4sGl(l!s system/mana)er Habc

LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL

Unix for Oracle Database Administrators

"I%

You might also like