• 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&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(" ");
    }
    //执行结果
    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;


    }
    }

  • 相关阅读:
    [bzoj1500][luogu2042][cogs339][codevs1758]维修数列(维护数列)
    无旋treap的简单思想以及模板
    [hdu2036]改革春风吹满地
    (treap)[bzoj3224][洛谷3369][cogs1829]Tyvj 1728 普通平衡树
    [bzoj3875][Ahoi2014]骑士游戏
    [bzoj1433][ZJOI2009]假期的宿舍
    <struct、union、enum>差异
    LeetCode(50) Pow(x,n)
    LeetCode(49)Group Anagrams
    LeetCode(48)Rotate Image
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/10278503.html
Copyright © 2020-2023  润新知