java调用shell脚本小demo
复制指定文件
cpp.sh:
[root@localhost soft]# vim cpp.sh
#!/bin/bash
name="$1"
\cp /home/soft/test/${name} /opt/
echo "co ok"
~
~
java:
public class YY {
public static void main(String[] args) throws IOException {
System.out.println("java linux starting 。。。。");
long start=System.currentTimeMillis();
//shell脚本地址
String filepath = "/home/soft/test";
String fileNmae=UUID.randomUUID().toString().replace("-","");
filepath=filepath+"/"+fileNmae;
File file = new File(filepath);
file.createNewFile();
OutputStream outputStream=new FileOutputStream(file);
String text="测试java调用shell脚本\n";
outputStream.write(text.getBytes());
outputStream.flush();
outputStream.close();
Runtime runtime = Runtime.getRuntime();
//shell脚本加参数 fileNmae
String path="/home/soft/cpp.sh "+fileNmae;
try {
Process ss = runtime.exec(path);
System.out.println("复制文件:"+filepath);
System.out.println(System.currentTimeMillis()-start);
System.out.println("java linux ending ....");
} catch (IOException e) {
e.printStackTrace();
System.out.println("java linux exception....");
}
}
}
执行效果:
[root@localhost soft]# java YY
java linux starting 。。。。
复制文件:/home/soft/test/0fcdde746dda4f588e404ab5ef9adec0
177
java linux ending ....
[root@localhost soft]# ll /opt/
total 8
-rw-r--r--. 1 root root 28 May 11 20:16 0fcdde746dda4f588e404ab5ef9adec0
drwxr-xr-x. 2 root root 4096 Nov 22 2013 rh
[root@localhost soft]#
java调用shell脚本小demo的更多相关文章
- Java 调用 shell 脚本详解
这一年的项目中,有大量的场景需要Java 进程调用 Linux的bash shell 脚本实现相关功能. 从之前的项目中拷贝的相关模块和网上的例子来看,有个别的“陷阱”造成调用shell 脚本在某些特 ...
- [转载]JAVA调用Shell脚本
FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...
- java调用shell脚本,并获得结果集的例子
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- java调用shell脚本
/** * 运行shell脚本 * @param shell 需要运行的shell脚本 */ public static void execShell(String shell){ try { Run ...
- JAVA调用shell脚本利用ansible修改多节点上的redis参数
创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...
- java调用shell脚本执行操作
//定时清空 日志 String shellString = "sh /home/jyapp/delete_log.sh"; Process process = Runtime.g ...
- Java 执行Shell脚本指令
一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...
- java基础/java调用shell命令和脚本
一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...
- 用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 touch sqoop_options.sh chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码传参数 ...
随机推荐
- Oracle Mysql MSSql 三种数据库 随机查询 条 语句
1. Oracle,随机查询查询语句-20条 select * from ( select * from 表名 order by dbms_random.value ) where rownum ...
- docker常用软件安装及使用
linux安装docker: sudo wget -qO- https://get.docker.com | sh 启动docker: service docker start 搜索镜像: docke ...
- win7旗舰版C盘无写入权限别拒绝怎么办? 精选
win7旗舰版C盘无写入权限别拒绝怎么办? 精选 https://zhidao.baidu.com/question/366277826663554972.html 浏览 42 次 1个回答 [热点话 ...
- Win8.1+VS2013+WDK8.1+VirtualBox or VMware 驱动开发环境配置
https://blog.csdn.net/charlessimonyi/article/details/50904956 Win8.1+VS2013+WDK8.1+VirtualBox or VMw ...
- 6.824 Lab 2: Raft 2C
Part 2C Do a git pull to get the latest lab software. If a Raft-based server reboots it should resum ...
- ARP协议基础
ARP 什么是ARP协议 ARP协议是能够根据IP地址解析出该IP地址所在设备的MAC地址,叫(Address Resolution Protocol)地址解析协议 ARP地址的工作流程 当一台主机A ...
- itchat监听微信撤回消息
import itchat from itchat.content import * import re msg_infomation = {} # 监听发送消息 @itchat.msg_regist ...
- MVC中的cshtml与ASPX的区别
在MVC3中,即可以使用cshtml,也可以使用aspx, 这两者到底有什么区别呢? 越详细越好,如果是用来正式开发,用哪种比较好. --------------------------------- ...
- 2019上海网络赛B题(差分 + 离散化 or 差分 + 思维)
这题.....队里都没怎么训练差分,导致败北...写了一堆线段树嘤嘤嘤,到最后也是超时,比赛结束后看到了差分的思想于是就去学了一手. 其实了解差分思想的一眼就能看出来是差分了.但是如果对n差分的话很明 ...
- SSIS包定时执行
企业管理器 --管理 --SQL Server代理 --右键作业 --新建作业 --"常规"项中输入作业名称 --"步骤"项 --新建 --"步骤名& ...