Linux环境下Java对进程的操作
来源:互联网 发布:如何申请淘宝 编辑:程序博客网 时间:2024/06/11 20:01
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 CMDpolkitd 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(); }
0 0
- Linux环境下Java对进程的操作
- java windows 下对进程的相关的操作
- linux环境下对mysql数据库的操作
- Linux下的进程环境
- c#下对进程的操作
- Wince 下对 进程的操作
- Windows下对进程的一些操作
- Linux中对进程的操作
- VC环境下对EXCEL的操作
- java对windows进程的操作
- Linux下对进程的监控
- linux下对进程与线程基本操作常用命令
- linux环境下的守护进程编写
- Linux环境下的进程控制
- Linux下进程操作
- 对进程的操作
- Linux环境下,db2 restore操作对文件系统的预分配
- java多线程环境下对变量的读写操作的原子性问题
- android opengl es基本操作,画线,三角形,正方形,颜色设置
- gradle环境变量配置
- 九度题目1202的解决
- java 解析 json
- C#实现窗口最小化到系统托盘
- Linux环境下Java对进程的操作
- Traversing an Array in Node.js
- ViewTreeObserver用法示例
- java IO
- iOS点方法使用
- 对象导论
- 关于Java构造器的一点归纳
- Window XP驱动开发(二) 环境搭建(VS2008+WDK+DDKWzard)及示例源码分析
- 复选框数据请求到后台