Script to determine OS process ID for timed-out sessions

Do you have trouble finding timed-out sessions and killing them? This script will help you find out the Unix process ID (PID) for the timed-out sessions. You can use this PID to kill the session from the operating system.

In the case of an app server malfunction or crash, sessions may not get closed properly. You may not be able to kill the session using the "alter system kill session" command. The best thing you can do is find out the UNIX process and kill it, which will in turn close the unwanted or timed-out session. This will also help to keep the total number of UNIX process under control.

I have found this script useful in OLTP databases, especially when you have connections from application servers or java clients. The total number of processess that can be spawned by the operating system may be excessive if the timed-out sessions' processes are active. (The total number of process in Solaris, for example, is decided by the parameter in the "/etc/system" file).

This script can be used to monitor the session status in detail and to control any user sessions that have the habit of not closing the connection properly. This query (which uses v$session and v$process) has been tested on Oracle 8.1.7 on Solaris 7 and 8.

    p.pid ppid,
    substr(s.program,1,20) USER_PROGRAME,
    s.process USER_PROCESS,
    substr(p.program,1,20) SERVER_PROGRAME,

    Requires Free Membership to View

to_char(p.spid) spid, to_char(logon_time, 'mm/dd/yy hh24:mi:ss') logon_time, -- idle time -- DAYS --( trunc(LAST_CALL_ET/86400) * 24 ) || ':' || -- days separately substr('0'||trunc(LAST_CALL_ET/86400),-2,2) || ':' || -- hours substr('0'||trunc(mod(LAST_CALL_ET,86400)/3600),-2,2) || ':' || -- minutes substr('0'||trunc(mod(mod(LAST_CALL_ET,86400),3600)/60),-2,2) || ':' || --seconds substr('0'||mod(mod(mod(LAST_CALL_ET,86400),3600),60),-2,2) idle_time from v$session s, v$process p where s.username is not null and p.addr(+) = s.paddr -- UNCOMMENT THE NEXT LINE TO SEE YOUR OWN SESSION ONLY --and userenv('SESSIONID') = s.audsid order by username, sid;

Reader Feedback

George T. writes: First, this is a very good script. However I was expecting (from the text of the tip) that it would only show the sessions that have timed out. It shows all user (not including Oracle's processes) sessions on my 8i database. The tip says "This script will help you find out the Unix process ID (PID) for the timed out sessions," which I understood to be only those sessions that have timed out. Is this correct? If so, maybe it is important to note that ACTIVE users will show up in the results of this script. You cannot pipe the results of this script to the 'kill'.

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free Oracle tips and scripts.
  • Tip contest: Have an Oracle tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical Oracle questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: Oracle tips, tutorials, and scripts from around the Web.

This was first published in November 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.