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代码传参数 ...
随机推荐
- CentOS 6.7与CentOS 7.3内存查看命令free、top的差异对比
https://www.centos.bz/2017/08/centos-6-7-centos-7-3-free-top/
- The file is inaccessible to Server.
ArcGIS Unable to Start serviceserver安装后,启动服务失败,报错信息如下:Unable to Start service. Error (Server object ...
- 分布式任务队列 Celery —— 应用基础
目录 目录 前文列表 前言 Celery 的周期定时任务 Celery 的同步调用 Celery 结果储存 Celery 的监控 Celery 的调试 前文列表 分布式任务队列 Celery 分布式任 ...
- 初学node.js-nodejs中实现用户注册路由
经过前面几次的学习,已经可以做下小功能,今天要实现的事用户注册路由. 一.users_model.js 功能:定义用户对象模型 var mongoose=require('mongoose'), S ...
- LSTM用于MNIST手写数字图片分类
按照惯例,先放代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 ...
- Grafana配置文件
配置文件位置 /usr/local/opt/grafana/share/grafana/default.ini /usr/local/etc/grafana/grafana.ini 配置文件调用顺序 ...
- 使用itchat获取微信好友的男女比例
# 使用itchat获取微信好友的男女比例 import itchat itchat.auto_login(hotReload=True) friends = itchat.get_friends(u ...
- Spring Security Session Time Out
最近在用Spring Security做登录管理,登陆成功后,页面长时间无操作,超过session的有效期后,再次点击页面操作,页面无反应,需重新登录后才可正常使用系统. 为了优化用户体验,使得在se ...
- TortoiseSVN-1.9.7 对应 eclipse svn 插件的 更新
用TortoiseSVN在文件夹导入项目之后,用eclipse 导入发现没有SVN信息,无法同步工程.出现这个情况先不管 TortoiseSVN的版本,直接把eclipse的svn版本升级到最新. H ...
- kafka使用问题解决
java.lang.UnsupportedClassVersionError:org/apach/kafka/comon/utils/Utils:Unsupport major.minor versi ...