Killing system process from java

By neokrates, written on May 20, 2010

java   snippet

Rate it
  • 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
Ad
Poll
  • Who manages CI environment in your enterprise?

    • We don't use CI (92%, 11 Votes)
    • Dedicated person per team, teams decide on their own (8%, 1 Votes)
    • Everyone who can (0%, 0 Votes)
    • Employee, single Release or Config Manager (0%, 0 Votes)
    • Contractor, single Release or Config Manager (0%, 0 Votes)
    • Team, Release or Config Management (0%, 0 Votes)
    • External partner manages our CI (0%, 0 Votes)
    • Different answer (0%, 0 Votes)

    Total Voters: 12

    Vote

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext
Most popular search terms:

This example explained here
 

import java.io.File;
import java.io.IOException;
 
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.io.FileUtils;
 
/**
 * This file tries to eliminate the existing 'firefox' process 
 * from java. It is an example. Change it into real java process
 * killer by adding parameters. extend it with windows cmd process
 * kill script etc..
 *
 * @author diuw
 * 
 */
public class JavaProcessKiller {
 private void run() {
 
  // Go for mysql(only current user process can be killed)
  String targetName = "firefox";
  String line = "kill -9 `ps -ef | grep " + targetName + " | awk '{print $2}'`;";
 
  // Create a tmp file. Write permissions!?
  try {
   FileUtils.writeStringToFile(new File("killProcess.bash" , line);
 
   // Execute the file we just creted. No flags are due if it is
   // executed with bash directly
   CommandLine commandLine = CommandLine.parse("bash killProcess.bash" ;
 
   try {
    DefaultExecutor executor = new DefaultExecutor();
    executor.execute(commandLine);
   } catch (ExecuteException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
 
  } catch (IOException e1) {
   e1.printStackTrace();
  }
 
 }
 
 public static void main(String[] args) {
  new JavaProcessKiller().run();
 }
 
}
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: +2 (from 2 votes)
2 votes 'YES'  0 votes 'NO'
TAGS
No tags for this post.
RELATED
Pages
Posts
SOCIAL
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • BlinkList
  • Blogosphere News
  • E-mail this story to a friend!
  • Furl
  • LinkArena
  • Live
  • MisterWong
  • Print this article!
  • StumbleUpon
  • Technorati
  • Webnews.de
  • YahooMyWeb

Leave a Reply