Have you ever tried to identify zombie processes on your server? The zombie identification application is written in java and could be used on linux. It is very easy to adopt the code for other operating systems. The zombie killer has been originally written to kill zombie processes on Oracle Application Server.
Go to the end if you are only interested how to end the financial crisis…;)
Application has two parameters:
- the process name is used to find out only the specific processes. In my example the parameter value is frmweb. Parameter value frmweb is used to limit the application to check only the cpu time spent by oracle forms processes
- the cpu time used by process is used to define the maximum allowed cpu time spent by particular process
How does it works:
- Application first get all the processes in desired format.
- ps -eo (to see every process with a user defined format)
- ps -eo user, pid, time, command (to see every process in format user, process id, time spent by process and command)
- Application checks only the processes where the parameter value for process name match the command (parameter value is in command string)
- Application parses the process values and check the cpu time spent by the process. Process is terminated if the cpu time spent is greater than the parameter value for cpu time.
Application could be run using crontab each 15 minutes. Example below shows how to kill oracle forms sessions which exceeds more than 1000 seconds of cpu time
0,15,30,45 * * * * /usr/java/jdk1.6.0_11/bin/java -cp . ZombieKiller frmweb 1000
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
/**
*
* @author http://www.javanus.com/blogs
*/
public class ZombieKiller {
public static String getUnparsedProcessList() {
InputStream in = null;
InputStream buffer = null;
Reader reader = null;
try {
//get all processes
Process p = Runtime.getRuntime().exec("ps -eo user,pid,time,command --sort stime");
in = p.getInputStream();
buffer = new BufferedInputStream(in);
reader = new InputStreamReader(buffer);
char[] charr = new char[1024];
StringBuffer strbuff = new StringBuffer();
while(true) {
int r = reader.read(charr);
if(r<=0) {
break;
}
strbuff.append(charr, 0, r);
}
return strbuff.toString();
}catch(IOException e) {
//todo - error handling
e.printStackTrace();
}
return null;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
System.out.println("Check out http://www.javanus.com/blogs && http://www.javanus.com/blog");
if (args.length==0) {
System.out.println("Usage: java -classpath . ZombieKiller
return;
}
String ls_processName = args[0];
int li_maxSecAllow = 9999;
//Is the maximum number of seconds defined
if (args.length>1 && args[1] != null) {
li_maxSecAllow = Integer.parseInt(args[1]);
}
String ls_procesi = getUnparsedProcessList();
while (ls_procesi.indexOf("\n")>=0) {
String ls_rowValue = ls_procesi.substring(0, ls_procesi.indexOf("\n"));
//parse row if process name match
if (ls_rowValue.indexOf(ls_processName)>=0) {
//user
String ls_user = ls_rowValue.substring(0, ls_rowValue.indexOf(" "));
ls_rowValue = ls_rowValue.substring(ls_rowValue.indexOf(" ")+1, ls_rowValue.length()).trim();
//pid
String ls_pid = ls_rowValue.substring(0, ls_rowValue.indexOf(" "));
ls_rowValue = ls_rowValue.substring(ls_rowValue.indexOf(" ")+1, ls_rowValue.length()).trim();
//time
String ls_time = ls_rowValue.substring(0, ls_rowValue.indexOf(" "));
//System.out.println(ls_time);
ls_time = ls_time.substring(ls_time.indexOf(":")+1, ls_time.length()).trim();
String ls_minute = ls_time.substring(0, ls_time.indexOf(":"));
//System.out.println("ls_minute="+ls_minute);
ls_time = ls_time.substring(ls_time.indexOf(":")+1, ls_time.length()).trim();
String ls_sek = ls_time;
//cmd
ls_rowValue = ls_rowValue.substring(ls_rowValue.indexOf(" ")+1, ls_rowValue.length()).trim();
String ls_cmd = ls_rowValue;//.substring(0, ls_rowValue.indexOf(" "));
//ali je noter ip adressa
int li_spentCputTime = Integer.parseInt(ls_minute)*60+Integer.parseInt(ls_sek);
//System.out.println(ls_cmd);
ls_rowValue = ls_rowValue.substring(ls_rowValue.indexOf(" ")+1, ls_rowValue.length()).trim();
//should I terminate this session?
if (li_spentCputTime>li_maxSecAllow) {
Process p = Runtime.getRuntime().exec("kill -9 " + ls_pid);
System.out.println("Process " + ls_pid + " has been successfully terminated!");
}
}
ls_procesi = ls_procesi.substring(ls_procesi.indexOf("\n")+1, ls_procesi.length());
}
}
}
It would be nice to end the financial crisis on the same way. I suggest to use kill -9 42 to end the economic depression. 42???
Do you find this post useful? Do not hesitate to leave a reply! Go to the bottom of this page.
Tags: cpu time spent by process, ias, identify the zombie process, java, kill process, linux, oracle application server, terminate zombie process, Zombie killer
Good Article.. It really helps…
Can you tell me whether scheduling how many zombies exist through C program.. means output the number of zombies process exists…
Nice code but this doesn’t kill zombies. Zombies is a previously killed process that wont die.