Oracle database Kill Session by UNIX/Windows/Oracle Command

** Credit web http://psoug.org/reference/kill_session.html 
UNIX

ps -ef | grep ora to find Oracle processes. Be sure to get the process id of the session you are trying to kill
Killing sessions in the UNIX environment


kill -1 <process_id>

or

kill -9 <process_id>

kill -9 5745
All in one kill
ps -ef | grep pmon_ | awk '{print $2}' | xargs kill -9

Windows

orakill <instance_name> <spid>
Killing sessions in the Windows environment with ORAKILL
SELECT instance_name

FROM gv;



col program format a30



SELECT spid, osuser, s.program, schemaname

FROM gv p, gv s

WHERE p.addr = s.paddr;



c:\oracle\product\ora102\bin> orakill orabase spid

All

To kill sessions within the database requires the ALTER SYSTEM privilege and the sid and serial# of the session to be killed
Killing sessions from inside the database


GRANT alter system TO <schema_name>;



SELECT sid, serial#, username, schemaname, osuser

FROM gv

WHERE username = <user_name>;



ALTER SYSTEM KILL SESSION '<sid>,<serial#>';

conn / as sysdba



GRANT alter system TO aqadmin;



conn aqadmin/aqadmin



SELECT sid, serial# , username, schemaname, osuser

FROM gv

WHERE username = 'AQUSER';



SQL> ALTER SYSTEM KILL SESSION '9,177' IMMEDIATE;

Kill All Sessions

conn / as sysdba
Kill All Instance Sessions


set heading off

set termout off

set verify off

set echo off

set feedback off



ALTER SYSTEM enable restricted session;



ALTER SYSTEM checkpoint global;



spool kill_all.sql



SELECT 'execute kill_session('|| chr(39) || sid || chr(39) || ',' || chr(39) || serial# || chr(39) || ');'

FROM gv_

WHERE (username IS NOT NULL OR username <> 'SYS');



spool off



@kill_all

Session Kill Demo Procedure

CREATE OR REPLACE PROCEDURE infinite_loop IS
An infinite loop for testing


BEGIN

  LOOP

    NULL;

  END LOOP;

END infinite_loop;

/



SQL> exec infinite_loop

0 comments:

Loading