How to kill Unix Processes
The Problem
Some CSIL Unix users experience either very slow response time from their system or applications that simply stop responding to user input. Slow response time from the system can be an indication of an ORPHANED or RUNAWAY processe/processes. FROZEN or HUNG processes can occur for numerous reasons. The situation can often be corrected simply by powering down the computer and restarting everything, but this can be a major problem if the affected computer has multiple users on it. To shut down the machine whenever a particular program stops responding properly could negatively affect other users on the system.
Orphaned processes can occur when software applications are not exited properly, i.e. applications terminate abnormally or unexpectedly. Orphaned processes can clog up the process table making the system eventually unusable. Runaway processes are especially probematic; they haven't exited abnormally, but they take up CPU cycles and other system resources (memory, disk space) which can result in sluggish performance from your computer. When a student is running an application that they or another student built and these conditions arise, the cause is normally due to a student programming error.
The Solution
Every Unix application is run by the O/S as a separate process. Each process is assigned a separate process ID (pid) and is stored in the process table while they are loaded. Once an application terminates normally, the O/S removes its pid from the process table and is then available to be re-assigned to a new process.
Orphaned/Runaway processes must be killed off. You can only kill off a process if you are the owner of that process, so it's important to clean up after yourself before you log off. Logging off will not necessarily terminate your orphaned/runaway processes. They may continue to execute, taking up system resources and slowing the machine down for other users. Keep in mind not just your local machine, but also any processes that you might have running on CSIL servers, like orion.csil.sfu.ca.
If you notice slow performance on a machine and you see orphaned/runaway processes that don't belong to you, please email helpdesk with as many details as you can.
How do I find out about Orphaned/Runaway processes?
The Unix utility ps can be used to find and terminate orphaned processes. Remember that all Unix commands have a man page, e.g.:
man ps
man kill
The following example assumes a userid of 'YourID'. substitute YOUR OWN userid for 'YourID':
First, list your processes:
ps -u YourID -f UID PID PPID C STIME TTY TIME CMD YourID 9772 9769 0 Feb 12 pts/3 0:01 /bin/csh YourID 9749 1 0 Feb 11 console 0:00 fbconsole YourID 11417 1 0 Feb 11 console 0:03 -csh YourID 9783 9780 0 Feb 12 pts/4 0:08 /bin/csh YourID 23843 9780 0 Feb 12 pts/10 0:08 /bin/csh YourID 23850 23849 0 13:57:48 pts/11 21:24 vhdbx YourID 23846 23843 0 13:56:58 pts/10 0:08 msgsvr -X -Y -Z YourID 29134 23843 0 17:23:21 pts/10 0:02 vhdbx YourID 29278 9772 0 17:31:01 pts/3 0:01 ps
- The first column is the Unix ID of the process owner.
- The 2nd column is the process ID (PID). This is the number you use in the kill command.
- The 3rd column is the parent process ID (PPID). Every process should have a parent process that it was started under, except the very 1st process. DO NOT USE THIS NUMBER TO KILL YOUR PROCESS(S)!!.
- The 4th column is the process start time (or date - if over a day old).
- The 5th column is the terminal where the process was started from.
- The second-to-last column is the CPU time used by the process so far.
- The program name is in the last column.
OK, I see my processes. Which ones are orphaned? Which ones are runaways?
An orphaned process is one shown by ps that you know you aren't using any more, you aborted (probably with a Ctrl-C, or it had its parent die unexpectedly (i.e. you closed a console window that an application was running in without exiting the application normally). The output of the ps command above shows that PID 23850 is a 2nd, older version of vhdbx. It has used a lot of cpu time (21+ minutes) and is hours old. Futhermore, it doesn't appear to have a parent process (PPID) currently running that is owned by YourID. PID 23850 might also be a runaway process, consuming significant CPU time and possibly other resources.
Although PID 23846 has a valid PPID, it is also hours old, and may not be currently running in a window that you can interact with. This process might be an orphaned process. It's probably not a runaway, as it doesn't seem to be taking up large amounts of CPU time.
How do I kill Orphaned/Runaway processes?
Use the kill command to delete processes from the process table. Once you know which one(s) you want to get rid of, note the process id (PID, not the PPID) of that process. You can use the kill utility to terminate them:
kill 23850
kill 23846
Another ps command should show these processes have been removed from the process table.
That's all there is to it?
Perhaps, but unfortunately, not all applications trap signals coming from the kill command. Applications must be specifically programmed to accept signals from the operating system (via kill). What really happens when you invoke the kill command is that a signal is sent to your application. kill allows the user to specify different signals to send to applications, but applications in turn must be programmed to listen for your kill signals as well. Some applications do listen for kill signals, some don't.
For example if you used kill like this:
kill -3 23850
The -3 tells the application running with PID to "quit". If it won't listen to that, try:
kill -1 23850
The -1 tells it to "hangup". If that doesn't work you can try:
kill -9 23850
"-9" is a drastic kill to be avoided whenever possible. Kill -9 doesn't allow a process to exit gracefully, no time to "clean up".
Having comments, suggestions, inquiries and more questions? Contact us!
Please click here and use the best practices to get assistance from helpdesk.
Last updated @ 2024.12.18