linux调用本地shell脚本
package com.haiyisoft.cAssistant.adapter.rest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.haiyisoft.cAssistant.common.StateEnum;
import com.haiyisoft.cAssistant.utils.JsonReturn;
import com.haiyisoft.cAssistant.utils.ShellUtil;
import com.haiyisoft.cAssistant.vo.ReturnValueVo;
//http://172.20.188.157:8011/cAssistant/getshell/exec.do?scriptPath=/data/app/cassistant/startbatch.sh¶=start,cAssistantrightweb
public class ShellController {
public static String execute(String scriptPath,String para){
try {
String[] params= para.split(",");
if(params.length>0){
for(int i=0;i<params.length;i++){
scriptPath+=" "+params[i];
}
}
String[] cmd = new String[]{"/bin/sh","-c",scriptPath};
//解决脚本没有执行权限
ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "777",scriptPath);
Process process = builder.start();
process.waitFor();
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
//执行结果
String result = sb.toString();
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
return " ";
}
}
//http://172.20.188.157:8011/cAssistant/getshell/mkdir.do?bakpath=/data/app/zzq&filepath=/data/app/filepath
@RequestMapping("/mkdir")
@ResponseBody
public ReturnValueVo mkdir(@RequestParam Map<String, String> map)
throws Exception {
String scriptPath=map.get("bakpath");
String filepath=map.get("filepath");
File file = new File(scriptPath);
if (!file.exists()) {//如果文件不存在
file.mkdir();
}
else{
String command3 = "rm -rf "+scriptPath;
String message3 = ShellUtil.runShell(command3);
file.mkdir();
System.out.println(message3);
}
ReturnValueVo result;
try {
/* String command1 = "chown zgc "+scriptPath;
String message1 = ShellUtil.runShell(command1);
System.out.println(message1);
String command2 = "chmod -R 777 "+scriptPath;
String message2 = ShellUtil.runShell(command2);
System.out.println(message2);
*/
String command3 = " cp -pr "+filepath +" "+scriptPath;
String message3 = ShellUtil.runShell(command3);
System.out.println(message3);
} catch (Exception e) {
// TODO: handle exception
result = JsonReturn.assemblyBean(null, StateEnum.FAIL.getStatus(),StateEnum.FAIL.getMsg());
return result;
}
result = JsonReturn.assemblyBean(null, StateEnum.SUCCESS.getStatus(),StateEnum.SUCCESS.getMsg());
return result;
}
}
最新文章
- Ruby之基本数据类型(三)
- CPU host-passthrough技术与应用
- ArcGIS API for Silverlight 之ElementLayer使用及TextSymbol的模板使用
- Android Non-UI to UI Thread Communications(Part 2 of 5)
- js基础例子dom+原型+oop基础知识记录01
- java Process在windows的使用汇总(转)
- 2015 QQ最新登录算法
- 基础数据结构-串-KMP算法
- 从Android源码的角度分析Binder机制
- 【JavaScript】设计模式-module模式及其改进
- c++(线性结构的处理)
- VS2013 重装 无法打开项目
- Python知识目录
- PhoneGap和Cordova应该用哪一个?
- 删除单链表节点,时间复杂度为O(1)
- [JLOI2014]聪明的燕姿(搜索)
- 使用cross-env解决跨平台设置NODE_ENV的问题
- Spring Boot集成JPA的Column注解命名字段无效的问题
- Linux网桥配置
- 免费json API