Dissecting Complex UNIX commands
UNIX
neophytes are often taken aback
when they see some of the
cryptic commands used in UNIX.
The pipe command in
particular lends to the
difficulty of reading UNIX
scripts.
Once the UNIX beginner
gets used to using the pipe
command, much of UNIX will
become more legible.
ps -ef|grep "ora_"|grep
$ORACLE_SID|-v grep| \ awk '{
print $2 }'|-exec rm –f {} \;
At
first glance, this command
appears to be a conglomeration
of cryptic letters.
However, upon closer
examination, this UNIX command
is actually a series of commands
that are joined together with
the “pipe” operator “|”.
When viewed this way, the
command can be viewed as a
connected list of commands:
ps –ef
|
grep "ora_"
|
grep $ORACLE_SID
|
grep -v grep
|
awk '{ print $2 }'
|
-exec rm –f {} \;
By
expanding the command onto
separate lines using the |
characters as a delimiter, one
is able to examine each
sub-command and see how each
successive command refines the
output from the prior UNIX
command (Figure 5).
Once the individual
commands that comprise the whole
UNIX script are revealed, begin
understanding each component.
Figure 5 shows that the
result set becomes smaller and
more refined with each
subsequent command.
Figure
5:
Dissecting a complex UNIX
command
Deciphering a Complex UNIX
Command
Utilize a one-line UNIX command
that is used to kill all Oracle
background processes for a
specified database on the UNIX
server as an example.
As an Oracle DBA, there
are times when it is necessary
to kill all Oracle processes, or
a selected sub-set of Oracle
processes.
This is a common UNIX
script used by an Oracle DBA who
wants to kill all Oracle
processes when the Oracle
database is “locked up” and the
database cannot be stopped with
the standard Oracle utilities.
The
basic format of the UNIX
kill
command is shown below, and a
single
kill
command can be used to kill many
UNIX processes (PIDs):
root> kill –9 process1 process2
process3
Thus,
the kill command can accept a
list of processes, and the goal
is to gather a list of processes
from UNIX and use them as
arguments to the kill command.
The following command will kill
all Oracle processes for the
server because of the –exec
syntax.
ps -ef|grep "ora_"|grep -v
grep|awk '{print $2}'|-exec kill
-9 {} \;
The steps within this command
are as follows:
1.
The
ps –ef
UNIX command displays all active
processes on the server, but,
the goal is to limit the command
to only those processes that are
running on the Oracle database.
2.
The
grep “ora_”
command removes all but the
Oracle processes:
root> ps -ef|grep "ora_"
oracle 13022
1
0
Sep 30
-
0:18 ora_db02_vald
oracle 14796 42726
0 09:00:46
pts/0
0:00 grep ora_
oracle 17778
1
0
Sep 30
-
0:14 ora_smon_devp
oracle 18134
1
0
Sep 30
-
0:37 ora_snp1_vald
oracle 19516
1
0
Sep 30
-
0:24 ora_db04_prod
oracle 21114
1
0
Sep 30
-
0:37 ora_snp0_devp
oracle 28436
1
0
Sep 30
-
0:18 ora_arch_prod
3.
The
grep –v grep is used to remove
the second line from the above
output.
The grep –v command is
used to exclude lines with the
specified string.
In the output below, note
that the grep line is now
missing from the output:
root> ps -ef|grep "ora_"|grep -v
grep
oracle 13022
1
0
Sep 30
-
0:18 ora_db02_vald
oracle 17778
1
0
Sep 30
-
0:14 ora_smon_devp
oracle 18134
1
0
Sep 30
-
0:37 ora_snp1_vald
oracle 19516
1
0
Sep 30
-
0:24 ora_db04_prod
oracle 21114
1
0
Sep 30
-
0:37 ora_snp0_devp
oracle 28436
1
0
Sep 30
-
0:18 ora_arch_prod
4.
Next,
use the UNIX awk command.
As discussed previously,
the awk or the cut commands can
be used to extract specific
columns from a result set.
In this case, use awk ‘{
print $2 }’ to get the second
column which is the Process ID
(or pid) for these processes.
This results in a list of
process ID’s to send to the kill
command.
root> ps -ef|grep "ora_"|grep -v
grep|awk '{ print $2 }'
13022 17778 18134 19516
21114 28436 28956
5.
Now
there is a clean list of process
IDs for the Oracle background
processes.
To ship the list to the
next command, pipe the list of
PIDs to the UNIX kill command by
using the –exec UNIX command.
The –exec command accepts
a list as an argument and
performs any UNIX command on the
input set.
Note:
If
HP/UX or AIX is being used, also
use the xargs command for this
purpose.
ps -ef|grep "ora_"|grep -v
grep|awk '{ print $2 }'|-exec
kill -9 {} \;
6.
Once
the complex command is built,
encapsulate the command into a
single UNIX alias.
alias nukem = “ps -ef|grep "ora_"|grep
-v grep| \
awk '{ print $2 }'|-exec
rm –f {} \;”
Entering the alias nukem alias
at the UNIX command prompt will
invoke the complex command to
kill all Oracle background
processes.
The example is for
illustration purposes only since
a prudent Oracle DBA would never
risk assigning such a dangerous
command to an alias, or at the
very least would require
multiple confirmations before
running the command.
Conclusion
The
purpose of this chapter has been
to provide an introduction to
the basic UNIX commands that are
used by the Oracle DBA.
Some of the most common
and useful UNIX commands have
been covered.
The
main points of this chapter
include:
§
Understanding the different
dialects of UNIX
§
The
syntax of common UNIX commands
§
Piping together multiple UNIX
commands
§
File
and directory permissions
§
Configuring the Oracle UNIX
environment
§
Deciphering complex UNIX
commands
This
book will continue with a closer
look at UNIX server management.
The next chapter will
cover some of the internal
mechanisms of the UNIX
architecture, and show the most
common UNIX command and
utilities that are used by the
Oracle DBA to monitor load on
the server.
|