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脚本的更多相关文章

  1. 如何在java程序中调用linux命令或者shell脚本

    转自:http://blog.sina.com.cn/s/blog_6433391301019bpn.html 在java程序中如何调用linux的命令?如何调用shell脚本呢? 这里不得不提到ja ...

  2. java 调用bash shell脚本阻塞的小问题的解决

    java  调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...

  3. Linux生产服务器Shell脚本分享

    Linux生产服务器Shell脚本分享 2012-6-6 86市场网 linux 作为一名Linux/unix系统管理员,我经常遇到人问这个问题:shell能做什么?PHP这么强大,为什么不用PHP来 ...

  4. Linux下添加shell脚本使得nginx日志每天定时切割压缩

    Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...

  5. linux下实现shell脚本自动连接mongodb数据库并创建索引

    在linux下创建shell脚本

  6. 【原】Java程序调用远程Shell脚本

    此程序的目的是执行远程机器上的Shell脚本. [环境参数]远程机器IP:192.168.234.123用户名:root密码:rootShell脚本的路径:/home/IFileGenTool/Bak ...

  7. Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数

    在Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数呢? 方法一:   . ./subscript.sh 方法二:   source ./subscript.sh 注意: 1.两个点之 ...

  8. 2019.11.13课堂实验之用Linux下的shell脚本完成两文本交替输出

    有两个文本如下,实际中并不知道两文本各有多少行: 文本1.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ccccccccccccccccccccccccccc ...

  9. shell 调用其他shell脚本中的变量、函数

    在Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数呢? 方法一:   . ./subscript.sh  (两个点之间,有空格) 方法二:   source ./subscript. ...

随机推荐

  1. NORDIC GATT事件

    假设有两个服务,每个服务注册相应事件 注册的事件为ble_dev_cfg_on_ble_evt.ble_lora_cfg_on_ble_evt 当在任何一个服务中发生GATT特征读或写的时候,注册的这 ...

  2. 使用 jenkins 为 nginx 增加上下文

    每次需要在Nginx增加上下文,都需要添加如下两段 ​ server.conf upstream serverdownloadPage { server 10.11.19.6:3023; } ​ ht ...

  3. Linux用户组管理及用户权限4

    权限管理:    ls -l        rwxrwxrwx:            左三位:定义user(owner)的权限            中三位:定义group的权限           ...

  4. PAT Basic 1021 个位数统计 (15 分)

    给定一个 k 位整数 1 (0, ,, d​k−1​​>0),请编写程序统计每种不同的个位数字出现的次数.例如:给定 0,则有 2 个 0,3 个 1,和 1 个 3. 输入格式: 每个输入包含 ...

  5. js excel导出功能

    <html> <head> <p style="font-size: 20px;color: red;">使用a标签方式将json导出csv文件 ...

  6. Psychos in a Line CodeForces - 319B (单调栈的应用)

    Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...

  7. Servlet中的乱码问题及解决办法

    假设现在有个form表单,当页面中提交一个包含中文的请求时,在服务端有可能出现中文乱码问题. <!DOCTYPE html> <html> <head> <m ...

  8. perl 数组变量(Array) 转载

    Perl 变量(2)--数组 原文地址:Perl 变量(2)--数组 作者:飞鸿无痕 二.数组 数组是标量数据的有序列表. 数组可以含任意多个元素.最小的数组可以不含元素,而最大的数组可以占满全部可用 ...

  9. sql 同一行中,不同结果在不同列显示

    对不同条件查询到的结果在同一行中展示 尝试写过使用","和inner join两种方式,感觉使用","的更加直观 select table1.guid, tab ...

  10. Confluence 6.15 附件(Attachments )宏

    附件宏显示添加到一个页面中的所有文件列表. 同时附件宏允许用户(用户需要具有正确的权限)来进行下面的操作: 直接从列表中上传一个文件到页面 编辑附件属性和标签 删除一个附件文件(这个将会删除文件的所有 ...