Skip to content

Instantly share code, notes, and snippets.

@simond
Created May 8, 2017 01:12
Show Gist options
  • Save simond/ef2c7439191531a593a106b0b7751f96 to your computer and use it in GitHub Desktop.
Save simond/ef2c7439191531a593a106b0b7751f96 to your computer and use it in GitHub Desktop.
Show a list of currently running oracle sessions using a shell script
#!/bin/ksh
#########################################################################
# Script Name : oracle_sessions.sh
# Notes : List all ACTIVE database sessions
#########################################################################
sqlplus -s <<- !
/ AS SYSDBA
SET LINES 160
SET PAGES 1000
SET LINESIZE 200
COL inst# FORMAT 9
COL username FORMAT A14
COL osuser FORMAT A09
COL fg_pid FORMAT A9
COL o_pid FORMAT A9
COL sid FORMAT 9999
COL serial# FORMAT 99999
COL program FORMAT A10 TRUNC
COL machine FORMAT A10 TRUNC
COL login FORMAT A18
COL service_name FORMAT A9
COL last_call FORMAT A10
COL wait FORMAT A27 TRUNC
-- COL duration FORMAT A10
COL wtime FORMAT A10
COL wsecs FORMAT 99999
SELECT s.inst_id inst#
, s.sid
, s.serial#
, s.username
-- , s.status
, s.osuser
, s.process fg_pid
, p.spid o_pid
, LTRIM(DECODE( s.osuser,
NULL , SUBSTR(p.program,
INSTR(p.program, '(') + 1,
LENGTH(p.program) - INSTR(p.program,'(') - 1),
s.program)) program
, s.machine
, s.service_name
-- , TO_CHAR(s.logon_time, 'DD-MON-YY HH24:MI:SS') login
, LPAD( DECODE( TRUNC(s.last_call_et/3600),
0, NULL,
LTRIM( TO_CHAR(TRUNC(s.last_call_et/3600), '90')) || 'h ') ||
DECODE( TRUNC(s.last_call_et/60),
0, NULL,
LPAD(LTRIM(TO_CHAR(MOD(TRUNC(s.last_call_et/60), 60), '90')), 2) ||
'm ') ||
LPAD(TO_CHAR(MOD(s.last_call_et, 60)), 2),
10) last_call
, sql_hash_value hash_value
, DECODE(w.state,'WAITING',w.EVENT,NULL) wait
, LPAD( DECODE( TRUNC(w.seconds_in_wait/3600),
0, NULL,
LTRIM( TO_CHAR(TRUNC(w.SECONDS_IN_WAIT/3600), '90')) || 'h ') ||
DECODE( TRUNC(w.SECONDS_IN_WAIT/60),
0, NULL,
LPAD(LTRIM(TO_CHAR(MOD(TRUNC(w.seconds_in_wait/60), 60), '90')), 2) ||
'm ') ||
LPAD(TO_CHAR(MOD(w.seconds_in_wait, 60)), 2),
10) wtime
FROM gv\$session s
, gv\$process p
, gv\$session_wait w
WHERE p.addr = s.paddr
AND s.inst_id = p.inst_id
AND s.inst_id = w.inst_id(+)
AND s.sid = w.sid(+)
AND s.status NOT IN ('INACTIVE')
-- Hide parallel slaves that are are waiting for the parent co-ordinator process
-- Slave will show as ACTIVE sessions, but are actually idle waiting for the parent
-- process
AND w.event NOT IN ('PX Deq: Execution Msg','PX Deq Credit: send blkd')
AND s.username IS NOT NULL
ORDER BY s.last_call_et DESC
, s.osuser
, s.username
, s.machine
, s.program
, s.logon_time;
!
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment