JAVA实现获取Linux下进程PID并杀掉进程
2017-02-22 12:40
579 查看
话不多说,直接上代码!
public class CloseLinuxProcess { public static void main(String[] args) { String PID = getPID("java -jar test.jar"); closeLinuxProcess(PID); } /** * 获取Linux进程的PID * @param command * @return */ public static String getPID(String 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)){ System.out.println("相关信息 -----> "+command); String[] strs = line.split("\\s+"); return strs[1]; } } }catch(Exception e){ e.printStackTrace(); }finally{ if(reader!=null){ try { reader.close(); } catch (IOException e) { } } } return null; } /** * 关闭Linux进程 * @param Pid 进程的PID */ public static void closeLinuxProcess(String Pid){ Process process = null; BufferedReader reader =null; try{ //杀掉进程 process = Runtime.getRuntime().exec("kill -9 "+Pid); reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while((line = reader.readLine())!=null){ System.out.println("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) { } } } } }
相关文章推荐
- JAVA分别实现Windows平台和Linux平台下的ip获取
- linux下获取指定exe进程pid和详细信息的命令
- FFmpeg在Linux下的简单安装并与Java结合获取视频缩略图功能实现
- 在linux环境下,java实现ftp下载获取文件对象数组总是失败的原因
- 通过获取系统进程快照获取进程pid以及杀进程(win下获取进程名和linux下获取进程路径)
- JAVA分别实现Windows平台和Linux平台下的ip获取
- Linux 父进程如何获取子进程返回值?parent get child process return value,fork,wait,wait_pid
- linux下根据进程名字获取PID,类似pidof(转)
- linux 下根据进程名字获取进程的进程号PID
- JAVA 获取当前JVM系统进程ID pid
- Linux开发已知进程名字获取PID号(C语言)
- java 调用 cmd 中的 tasklist 来获取特定进程的PID
- 进程名字获取进程的进程号PID,类似pidof,C函数,适合linux,AIX和hp-unix平台
- JAVA分别实现Windows平台和Linux平台下的ip获取
- 获取Linux执行命令后的PID--管理部署在服务器的Java程序
- Linux下启动Java进程并获得进程ID(PID)
- 获取当前运行Java的进程标识符(PID)
- 如何获取linux下的进程pid列表
- Linux 精准获取进程pid--转
- JAVA分别实现Windows平台和Linux平台下的ip获取