shell腳本運行java
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException,InterruptedException {
//Runtime.getRuntime().exec("/Users/lijialiang/codetest/stop.sh jmeter");
//Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","/codetest/stop.sh jmeter"});
//Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep jmeter | grep -v 'grep' |awk '{print $2}'>>result.txt"});
Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep <span style="font-family: Arial, Helvetica, sans-serif;">jmeter</span><span style="font-family: Arial, Helvetica, sans-serif;"> | awk '{print $2}' | xargs kill -9"});</span>
//Thread.sleep(100000);
}
}
Ⅱ 如何在java中執行shell腳本
// 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102"; /參數
String command2 = 「/bin/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();
Ⅲ 怎麼在shell腳本中運行java代碼
// 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102"; /參數
String command2 = 「/bin/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();
Ⅳ 如何在java中執行shell腳本
過CommandHelper.execute方法可以執行命令,該類實現
復制代碼代碼如下:
package javaapplication3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author chenshu
*/
public class CommandHelper {
//default time out, in millseconds
public static int DEFAULT_TIMEOUT;
public static final int DEFAULT_INTERVAL = 1000;
public static long START;
public static CommandResult exec(String command) throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec(command);
CommandResult commandResult = wait(process);
if (process != null) {
process.destroy();
}
return commandResult;
}
private static boolean isOverTime() {
return System.currentTimeMillis() - START >= DEFAULT_TIMEOUT;
}
private static CommandResult wait(Process process) throws InterruptedException, IOException {
BufferedReader errorStreamReader = null;
BufferedReader inputStreamReader = null;
try {
errorStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
inputStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//timeout control
START = System.currentTimeMillis();
boolean isFinished = false;
for (;;) {
if (isOverTime()) {
CommandResult result = new CommandResult();
result.setExitValue(CommandResult.EXIT_VALUE_TIMEOUT);
result.setOutput("Command process timeout");
return result;
}
if (isFinished) {
CommandResult result = new CommandResult();
result.setExitValue(process.waitFor());
//parse error info
if (errorStreamReader.ready()) {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = errorStreamReader.readLine()) != null) {
buffer.append(line);
}
result.setError(buffer.toString());
}
//parse info
if (inputStreamReader.ready()) {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = inputStreamReader.readLine()) != null) {
buffer.append(line);
}
result.setOutput(buffer.toString());
}
return result;
}
try {
isFinished = true;
process.exitValue();
} catch (IllegalThreadStateException e) {
// process hasn't finished yet
isFinished = false;
Thread.sleep(DEFAULT_INTERVAL);
}
}
} finally {
if (errorStreamReader != null) {
try {
errorStreamReader.close();
} catch (IOException e) {
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
}
}
}
}
}
CommandHelper類使用了CommandResult對象輸出結果錯誤信息。該類實現
復制代碼代碼如下:
package javaapplication3;
/**
*
* @author chenshu
*/
public class CommandResult {
public static final int EXIT_VALUE_TIMEOUT=-1;
private String output;
void setOutput(String error) {
output=error;
}
String getOutput(){
return output;
}
int exitValue;
void setExitValue(int value) {
exitValue=value;
}
int getExitValue(){
return exitValue;
}
private String error;
/**
* @return the error
*/
public String getError() {
return error;
}
/**
* @param error the error to set
*/
public void setError(String error) {
this.error = error;
}
}
Ⅳ 如何在java中執行shell腳本
在java中執行shell腳本 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102"; /參數
String command2 = 「/bin/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();
Ⅵ 如何在java中執行shell腳本
使用shell腳本啟動zookeeper步驟:採用shell腳本啟動zookeeper,首先新建文件start.sh寫入內容(rh1rh2rh3分別是主機名。此處需要ssh):#!/bin/shecho「startzkServer…」foriinrh1rh2rh3dossh$i「/usr/local/zookeeper3.4/bin/zkServer.shstart」done寫好後保存,加上執行許可權:chmo+xstart.sh運行:./start.sh看見啟動成功了,有輸出。但是輸入jps查看的時候,會發現沒有QuorumPeerMain進程。說明沒有啟動成功。分析原因首先知道互動式shell和非互動式shell、登錄shell和非登錄shell是有區別的在登錄shell里,環境信息需要讀取/etc/profile和~/.bash_profile,~/.bash_login,and~/.profile按順序最先的一個,並執行其中的命令。除非被—noprofile選項禁止了;在非登錄shell里,環境信息只讀取/etc/bash.bashrc和~/.bashrc手工執行是屬於登陸shell,腳本執行數據非登陸shell,而我的linux環境配置中只對/etc/profile進行了jdk1.6等環境的配置,所以腳本執行/usr/local/zookeeper3.4/bin/zkServer.shstart啟動zookeeper失敗了解決方法把profile的配置信息echo到.bashrc中echo『source/etc/profile』~/.bashrc在/zookeeper/bin/zkEnv.sh的中開始位置添加exportJAVA_HOME=/usr/local/jdk1.6(就像hadoop中對hadoop-env.sh的配置一樣)採用shell腳本啟動zookeeper,首先新建文件start.sh寫入內容(rh1rh2rh3分別是主機名。此處需要ssh):#!/bin/shecho「startzkServer就可以了。
Ⅶ linux shell腳本如何啟動一個java進程
在shell裡面直接調用即可。
1,編譯一個java文件為a.class。
2,編寫shell腳本b
.sh
。
#!/bin/bash
java a.class 3,放在同一目錄下運行shell即可。如果要後台運行,bash
b.sh&
Ⅷ java怎麼執行shell腳本
如果shell腳本和java程序運行在不同的伺服器上,可以使用遠程執行Linux命令執行包,使用ssh2協議連接遠程伺服器,並發送執行命令就行了,ganymed.ssh2相關mave配置如下,你可以自己網路搜索相關資料。
如果shell腳本和java程序在同一台伺服器上,
這里不得不提到java的process類了。
process這個類是一個抽象類,封裝了一個進程(你在調用linux的命令或者shell腳本就是為了執行一個在linux下執行的程序,所以應該使用process類)。
process類提供了執行從進程輸入,執行輸出到進程,等待進程完成,檢查進程的推出狀態,以及shut down掉進程。
<dependency>
<groupId>com.ganymed.ssh2</groupId>
<artifactId>ganymed-ssh2-build</artifactId>
<version>210</version>
</dependency>
本地執行命令代碼如下:
Stringshpath="/test/test.sh";//程序路徑
Processprocess=null;
Stringcommand1=「chmod777」+shpath;
process=Runtime.getRuntime().exec(command1);
process.waitFor();
Ⅸ 如何在linux系統下編寫shell腳本來運行一個javaprogram,,求大神!!
在linux 下編寫shell 腳本運行 java程序和在windows的命令行運行程序是一樣的。
命令行運行java程序的寫法:
編譯:javac xxx.java
執行:java xxx
在linux下相同:
1. 先vi一個shell腳本文件: vi executejava.sh
2. 文件的內容就把剛才的兩句話寫進去就ok了。
3. 執行的話直接:sh excecutejava.sh
Ⅹ 如何在java中執行shell腳本
// 用法:Runtime.getRuntime().exec("命令");
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
try {
Runtime.getRuntime().exec(command1 ).waitFor();
} catch (IOException e1) {
e1.printStackTrace();
}catch (InterruptedException e) {
e.printStackTrace();
}
String var="201102"; /參數
String command2 = 「/<a href="https://www..com/s?wd=bin&tn=44039180_cpr&fenlei=-uH-Wn1nsPHFbmyP--bIi4WUvYETgN-" target="_blank" class="-highlight">bin</a>/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();