Java對Linux進程關閉


 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import com.talkweb.thread.SignatureSynthesisThread;

/**
 * 操作Linux進程工具類
 * 
 * @author Administrator
 *
 */
public class LinuxProcessUtil {

    private static Logger logger = Logger.getLogger(LinuxProcessUtil.class);
    
    /**
     * 獲取指定進程的PID
     * 
     * @param command
     * @return
     */
    public static String getPID(String command) throws Exception {
        logger.info("相關信息 -----> " + command);
        BufferedReader reader = null;
        try {
            // 顯示所有進程
            Process process = Runtime.getRuntime().exec("ps -ef");
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.contains(command)) {
                    logger.info("找到有關進程:"+line);
                    String[] strs = line.split("\\s+");
                    return strs[1];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {

                }
            }
        }
        logger.info("找不到有關進程pid返回null");
        return null;
    }

    /**
     * 關閉Linux進程
     * 
     * @param Pid 進程的PID
     */
    public static void closeLinuxProcess(String Pid) {
        Process process = null;
        BufferedReader reader = null;
        try {
            // 殺掉進程
            logger.info("准備執行 kill -9 " + Pid);
            process = Runtime.getRuntime().exec("kill -9 " + Pid);
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null) {
                logger.info("kill PID return info -----> " + line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {

                }
            }
        }
    }

}

 

package com.talkweb.utils;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;
import org.apache.log4j.Logger;
import com.talkweb.thread.SignatureSynthesisThread;
/** * 操作Linux進程工具類 *  * @author Administrator * */public class LinuxProcessUtil {
private static Logger logger = Logger.getLogger(LinuxProcessUtil.class);/** * 獲取指定進程的PID *  * @param command * @return */public static String getPID(String command) throws Exception {logger.info("相關信息 -----> " + command);BufferedReader reader = null;try {// 顯示所有進程Process process = Runtime.getRuntime().exec("ps -ef");reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;while ((line = reader.readLine()) != null) {if (line.contains(command)) {logger.info("找到有關進程:"+line);String[] strs = line.split("\\s+");return strs[1];}}} catch (Exception e) {e.printStackTrace();throw e;} finally {if (reader != null) {try {reader.close();} catch (IOException e) {
}}}logger.info("找不到有關進程pid返回null");return null;}
/** * 關閉Linux進程 *  * @param Pid 進程的PID */public static void closeLinuxProcess(String Pid) {Process process = null;BufferedReader reader = null;try {// 殺掉進程logger.info("准備執行 kill -9 " + Pid);process = Runtime.getRuntime().exec("kill -9 " + Pid);reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;while ((line = reader.readLine()) != null) {logger.info("kill PID return info -----> " + line);}} catch (Exception e) {e.printStackTrace();} finally {if (process != null) {process.destroy();}
if (reader != null) {try {reader.close();} catch (IOException e) {
}}}}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM