我们经常有需要从SHELL里面传入参数,到Sqlplus里面执行命令。
比如我跑一个Top进程,看到某个进程非常消耗资源。
Code:
Oracle@main-db1$top
last pid:4327;load averages:1.81,2.13,2.11
536 processes: 534 sleeping, 2 on cpu
CPU states: % idle, % user, % kernel, % iowait, % swap
Memory: 12G real, 373M free, 6853M swap in use, 7235M swap free
PID USERNAME THR PRI NICESIZE RES STATETIMECPU COMMAND
26240 oracle 159-20 5592M 5569M sleep134:100.71% oracle
6219 oracle 159-20 5593M 5569M sleep 36:510.63% oracle
5717 oracle 259-20 5595M 5572M sleep 35.6H0.48% oracle
26314 oracle 159-20 5593M 5572M sleep 27.4H0.34% oracle
5553 oracle 159-20 5592M 5570M sleep910:480.33% oracle
22514 oracle 159-20 5592M 5569M sleep329:320.31% oracle
6229 oracle 159-20 5592M 5568M sleep 16:000.28% oracle
27995 oracle 159-20 5593M 5569M sleep7:460.28% oracle
17843 oracle 259-20 5595M 5572M sleep594:580.27% oracle
我想很快看到这个进程是什么,手工进入SQLPLUS,根据v$session, v$process一个一个join,非常麻烦。
最好能够从shell 里面直接输入spid,马上让我看到这个进程是什么,在干什么。
下面是我写的一个简单例子,
Code: [Copy to clipboard]
sqlplus /nolog <<EOF
connect / as sysdba
col machine format a30
col program format a40
set line 200
select sid,serial# ,username,osuser,machine,program,process,to_char(logon_time,'yyyy/mm/dd hh24:mi:ss')
from v\$session where paddr in
( select addr from v\$process where spid in($1));
select sql_text from v\$sqltext_with_newlines
where hash_value in
(select SQL_HASH_VALUE from v\$session where
paddr in (select addr from v\$process where spid=$1)
)
order by piece;
exit;
EOF
有了这个简单例子之后,我可以很方便,及时的看到这个进程在干什么:
Code: [Copy to clipboard]
oracle@main-db1$./whoisit.sh 26240
SQL*Plus: Release 8.1.7.0.0 - ProdUCtion on Sat Dec 14 15:19:55 2002
(c) Copyright 2000 Oracle Corporation.All rights reserved.
15:19:55 SQL Connected.
15:19:55 SQL 15:19:55 SQL 15:19:55 SQL 15:19:55 SQL 15:19:55 215:19:55 3
SIDSERIAL# USERNAME OSUSER MACHINEPROGRAMPROCESS TO_CHAR(LOGON_TIME,
---------- ---------- ------------------------------ ------------------------------ ------------------------------ ---------------------------------------- --------- -------------------
40462253APP1app1mid1?
@mid1 (TNS V1-V3)60952002/12/13 16:00:40
1 row selected.
Elapsed: 00:00:00.02
15:19:55 SQL 15:19:55 SQL 15:19:55 215:19:55 315:19:55 415:19:55 515:19:55 6
SQL_TEXT
----------------------------------------------------------------
SELECT * FROM RC_REQUESTCCPAYMENTWHERE UL_LOGINNAME = :b1A
ND RC_POSTDATE = TO_DATE(:b2,'YYYYMM')AND RC_POSTDATE < ADD_M
ONTHS(TO_DATE(:b2,'YYYYMM'),1)AND RC_STATUS < 3000 ORDER BY RC
_REQUESTCCPAYMENTID DESC
4 rows selected.
Elapsed: 00:00:00.01
15:19:55 SQL 15:19:55 SQL Disconnected from Oracle8i Enterprise Edition Release 8.1.7.2.0 - 64bit Production
With the Partitioning option
JServer Release 8.1.7.2.0 - 64bit Production