Linux环境下Java对进程的操作

2/22/2017来源:ASP.NET技巧人气:1447

linux环境下java对进程的操作

本次主要讨论linux底下java对进程是否存在进行判断,以及java如何执行Linux脚本和命令

java执行Linux脚本和命令

java要执行Linux脚本首先得获取java的运行环境runtime

/** * 执行linux命令 * @auther liduote * 2017-2-21 下午04:41:28 * @param runtime * @throws IOException */ public static void execCMD(Runtime runtime) throws IOException { runtime.exec("ls -al"); }

java判断某进程是否存在

tips:ps 命令使用方法 ps [-aAcdefHjlmNVwy][acefghLnrssTuvxX][-C <指令名称>][-g <群组名称>] [-G <群组识别码>][-p <进程识别码>][p <进程识别码>][-s <阶段作业>] [-t <终端机编号>][t <终端机编号>][-u <用户识别码>][-U <用户识别码>] [U <用户名称>][-<进程识别码>][–cols <每列字符数>] [–columns <每列字符数>][–cumulative][–deselect][–forest] [–headers][–help][– info][–lines <显示列数>][–no-headers] [–group <群组名称>][-Group <群组识别码>][–pid <进程识别码>] [–rows <显示列数>][–sid <阶段作业>][–tty <终端机编号>] [–user <用户名称>][–User <用户识别码>][–version] [–width <每列字符数>]

#假如查询MySQL $ ps -C mysqld -f --columns=200 ------------------------------- UID PID PPID C STIME TTY TIME CMD polkitd 24995 11152 0 Jan12 ? 00:21:22 mysqld --init-file=/tmp/mysql-first-time.sql

所以我们通过java代码的写法是

public boolean isExistPRocess() throws IOException { String command = "ps -C mysqld -f --cols=200"; String program = "mysqld --init-file=/tmp/mysql-first-time.sql"; boolean isExistProcess = false; Process process = Runtime.getRuntime().exec(command); InputStream in = process.getInputStream(); InputStreamReader is = new InputStreamReader(in); BufferedReader read = new BufferedReader(is); String line = ""; while((line = read.readLine()) != null) { if(line.indexOf(program) >= 0) { isExistProcess = true; break; } } //关闭流 in.close(); is.close(); read.close(); process.destroy(); return isExistProcess; }

综合起来使用可以对apache(tomcat)进行重启

//软件安装目录 public static final String CATALINA_HOME = "/data/app/tomcat"; //重试次数 public static final int RETRY_TIME = 10; /** * 重启Tomcat * @auther liduote * 2017-2-21 下午05:27:24 * @param runtime * @return * @throws IOException */ public static boolean restartTomcat(Runtime runtime) throws IOException{ //结束tomcat进程 for (int i = 0; i < RETRY_TIME; i++) { if(isExistTomcatProcess(runtime)) { //调用tomcat自身脚本结束进程 shutdownTomcat(runtime); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } if(isExistTomcatProcess(runtime)) { //强制结束进程 killTomcatBySoft(runtime); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }else { break; } } //启动tomcat for (int i = 0; i < RETRY_TIME; i++) { if(!isExistTomcatProcess(runtime)) { //调用tomcat自身脚本重启程序 startupTomcat(runtime); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } }else { break; } } if(isExistTomcatProcess(runtime)) { return true; }else { return false; } } /** * 判断是否含有tomcat进程 * @auther liduote * 2017-2-21 下午04:41:04 * @param runtime * @return * @throws IOException */ public static boolean isExistTomcatProcess(Runtime runtime) throws IOException { String program = "org.apache.catalina.startup.Bootstrap start"; String command = "ps -C java -f --cols=1000"; return isExistProcess(runtime, command, program); } /** * 判断是否含有apache进程 * @auther liduote * 2017-2-21 下午04:41:04 * @param runtime * @return * @throws IOException */ public static boolean isExistApacheProcess(Runtime runtime) throws IOException { String program = "/usr/sbin/httpd"; String command = "ps -C httpd -f --cols=1000"; return isExistProcess(runtime, command, program); } /** * 判断当前进程中是否含有program * @auther liduote * 2017-2-21 下午05:41:04 * @param runtime * @param command * @param program * @return * @throws IOException */ public static boolean isExistProcess(Runtime runtime, String command, String program) throws IOException { boolean isExistTomcatProcess = false; Process process = runtime.exec(command); InputStream in = process.getInputStream(); InputStreamReader is = new InputStreamReader(in); BufferedReader read = new BufferedReader(is); String line = ""; while((line = read.readLine()) != null) { if(line.indexOf(program) >= 0) { isExistTomcatProcess = true; break; } } //--------------------- in.close(); is.close(); read.close(); process.destroy(); //--------------------- return isExistTomcatProcess; } /** * 关闭Tomcat * @auther liduote * 2017-2-21 下午04:41:04 * @param runtime * @throws IOException */ public static void shutdownTomcat(Runtime runtime) throws IOException { runtime.exec(CATALINA_HOME + "bin/shutdown.sh"); } /** * 启动Tomcat * @auther liduote * 2017-2-21 下午04:41:04 * @param runtime * @throws IOException */ public static void startupTomcat(Runtime runtime) throws IOException { runtime.exec(CATALINA_HOME + "bin/startup.sh"); } /** * 重启Apache * @auther liduote * 2017-2-21 下午04:41:04 * @param runtime * @return * @throws IOException */ public static boolean restartApache(Runtime runtime) throws IOException { if(isExistApacheProcess(runtime)) { runtime.exec("/usr/sbin/httpd -k restart"); }else { runtime.exec("/usr/sbin/httpd start"); } if (isExistApacheProcess(runtime)){ return true; }else{ return false; } } /** * 强制结束Tomcat进程 * @auther liduote * 2017-2-21 下午04:41:04 * @param runtime * @throws IOException */ public static void killTomcatBySoft(Runtime runtime) throws IOException { String[] cmd = {"sh", "-c", "ps aux | grep tomcat"}; Process process = runtime.exec(cmd); InputStream in = process.getInputStream(); InputStreamReader is = new InputStreamReader(in); BufferedReader read = new BufferedReader(is); String line = null; while((line = read.readLine()) != null) { if(line.indexOf("org.apache.catalina.startup.Bootstrap start") >= 0) { String tomcatPid = line.split("\\s+")[1]; runtime.exec("kill -9 "+tomcatPid); } } in.close(); is.close(); read.close(); process.destroy(); }