set serveroutput on
DECLARE
sqlstatement VARCHAR2(200);
BEGIN
  FOR i in (select * from gv$session s 
            where s.program like 'emagent@%'
              and s.status = 'ACTIVE' and s.last_call_et > 300)
  LOOP
     BEGIN
       sqlstatement := 'alter system kill session ''' || i.sid || ',' || i.serial# || ',@' || i.inst_id || ''' immediate';
       dbms_output.put_line(sqlstatement);
       execute immediate sqlstatement;
     EXCEPTION
         WHEN OTHERS THEN
            NULL;
            DBMS_OUTPUT.PUT_LINE(sqlerrm);
     END;          
  END LOOP;
END;
/


