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;
}
}
linux调用本地shell脚本的更多相关文章
- 如何在java程序中调用linux命令或者shell脚本
转自:http://blog.sina.com.cn/s/blog_6433391301019bpn.html 在java程序中如何调用linux的命令?如何调用shell脚本呢? 这里不得不提到ja ...
- java 调用bash shell脚本阻塞的小问题的解决
java 调用bash shell脚本阻塞的小问题的解决 背景 使用java实现的web端,web端相应用户的界面操作,使用java调用bash实现的shell脚本进行实际的操作,操作完成返回执行结 ...
- Linux生产服务器Shell脚本分享
Linux生产服务器Shell脚本分享 2012-6-6 86市场网 linux 作为一名Linux/unix系统管理员,我经常遇到人问这个问题:shell能做什么?PHP这么强大,为什么不用PHP来 ...
- Linux下添加shell脚本使得nginx日志每天定时切割压缩
Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...
- linux下实现shell脚本自动连接mongodb数据库并创建索引
在linux下创建shell脚本
- 【原】Java程序调用远程Shell脚本
此程序的目的是执行远程机器上的Shell脚本. [环境参数]远程机器IP:192.168.234.123用户名:root密码:rootShell脚本的路径:/home/IFileGenTool/Bak ...
- Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数
在Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数呢? 方法一: . ./subscript.sh 方法二: source ./subscript.sh 注意: 1.两个点之 ...
- 2019.11.13课堂实验之用Linux下的shell脚本完成两文本交替输出
有两个文本如下,实际中并不知道两文本各有多少行: 文本1.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ccccccccccccccccccccccccccc ...
- shell 调用其他shell脚本中的变量、函数
在Shell中要如何调用别的shell脚本,或别的脚本中的变量,函数呢? 方法一: . ./subscript.sh (两个点之间,有空格) 方法二: source ./subscript. ...
随机推荐
- JVM垃圾回收那些事
Java这种VM类跨平台语言比起C++这种传统编译型语言很大的区别之一在于引入了垃圾自动回收机制.自动垃圾回收大大提高了Java程序员的开发效率并且极大地减少了犯错的概率,但终归而言由于无法像C++程 ...
- Java学习笔记【十一、序列化】
序列化的条件 实现Serializable接口 所有属性必须是可序列化的,或标记为transient(不做序列化) 序列化-将对象输出为序列化文件 ObjectOutputStream 反序列化-将序 ...
- Delphi RS-232C标准
- 8、nginx基础
1Nginx基本简述 Nginx是一个开源且高性能.可靠的Http Web服务.代理服务. 开源: 直接获取源代码 高性能: 支持海量并发 可靠: 服务稳定 我们为什么选择 Nginx服务 Nginx ...
- Linux FTP 命令全集
Linux FTP 命令全集 1 前言 下面就所有命令给出解释和例子. 说明: 1. remote-file 指远程文件,即服务器上的文件 2. local-file 指本地文件,即本地机器上的文 ...
- IIS无法启动解决方案
1.第一步 2.第二步
- jsp根据某一行颜色来其他行的颜色
jsp根据某一行颜色(单选框)来其他行的颜色 <c:choose> <c:when test="${v.color=='黑色' }"> <td sty ...
- 对webview的研究--------引用
简要的说,webview能够在移动应用中开辟出一个窗口,在里面显示html页面,css以及js代码也可以被解析执行,它使用的是我们熟悉的webkit内核.android和ios都有相应的API,所以写 ...
- BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 概率与期望+高斯消元
这个还挺友好的,自己相对轻松能想出来~令 $f[i]$ 表示起点到点 $i$ 的期望次数,则 $ans[i]=f[i]\times \frac{p}{q}$ #include <cmath> ...
- poj 2976 Dropping tests (最大化平均值:二分查找)
#include<iostream> #include<algorithm> #include<stdio.h> #include<math.h> #d ...