码迷,mamicode.com
首页 > 系统相关 > 详细

linux调用本地shell脚本

时间:2019-01-16 19:24:32      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:spring   .json   ons   reader   null   path   cas   class   input   

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&para=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;


}
}

linux调用本地shell脚本

标签:spring   .json   ons   reader   null   path   cas   class   input   

原文地址:https://www.cnblogs.com/zhangzhiqin/p/10278503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!