java 执行linux命令或shell脚本方法

本文探讨了Java程序调用Shell脚本时常见的错误及其解决办法,包括命令执行问题、路径问题、进程同步问题及如何正确获取命令执行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.容易出错的地方之一,执行ps等命令要注意到jvm调用的linux命令也是一个进程

背景:第1,2,3,4行代码都可以完成java 执行shell脚本的任务,第2,3,4行代码的数组中第三个参数可以是脚本名,也可以是cd / ls / chown 这种命令。

问题:本例中调用其他shell脚本和linux命令都正常,而在第四行代码中调用ps时候,会遇到不能执行的问题

import java.io.IOException;

public class test {
    public static void main(String[] args) throws IOException,InterruptedException {
        //Runtime.getRuntime().exec("/Users/lijialiang/codetest/stop.sh jmeter");
        //Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","/codetest/stop.sh jmeter"});
        //Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep jmeter | grep -v 'grep' |awk '{print $2}'>>result.txt"});
       	Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","ps -ef | grep <span style="font-family: Arial, Helvetica, sans-serif;">jmeter</span><span style="font-family: Arial, Helvetica, sans-serif;"> | awk '{print $2}' | xargs kill -9"});</span>
        //Thread.sleep(100000);
        }
}

java代码中,第一行和第二行等价,都是调用stop.sh脚本,第三行第四行等价,都是直接执行linux命令

在此处第一句第二句总能拿到两个pid,并且当前stop.sh脚本进程也被kill调。而java的第3行和第4行能正确执行,只拿到1个pid,而且kill掉jmeter进程后,stop.sh进程

仍然正常存在。

stop.sh如下:

pid=`ps -ef | grep jmeter | grep -v 'grep' |awk '{print $2}'`
echo ${pid}>>result.txt
ps -ef | grep jmeter | grep -v 'grep'| awk '{print $2}' | xargs kill -9 

原因:java代码调用shell脚本和直接执行shell脚本里的code,在此处不等效。stop.sh的第1句执行,时而拿到1个pid,时而拿到2个的原因是,在java中直接调用stop.sh,jvm进程,stop.sh进程,ps -ef 和被判断的jmeter进程,共存在这四个进程。在java中直接调用ps -ef命令,则只存在jvm和ps -ef 和jmeter这三个进程,这时,ps -ef 不止能判断出jmeter进程,还能把stop.sh jmeter这个进程的pid拿出来并且kill掉,以上问题解决。




二.容易出错的地方之二,路径问题

     这个linux命令被jvm或者tomcat启动,那么这个命令的进程所在路径是他的父进程的路径(classpath),此时linux命令中和路径有关的命令,要注意。


三.容易出错的地方之三,父进程执行完就结束,子进程还没执行完就被迫结束

     如果java中执行‘nohup  run.sh   30min   &’ 这种命令,命令本身需要30分钟能执行完,但是java 执行nohup这句一瞬间执行完,所代表的父进程结束,则nohup子进程也结束。如果不想被结束,就不要用这种很特殊的nohup命令,而直接在java中调用' run.sh 30min '命令即可。


四.打印执行命令的结果

public static void main(String[] args) throws IOException {
		 String line = "";
		 Process ps = Runtime.getRuntime().exec("ls");
		 BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
		 while((line = br.readLine())!=null ){  
		     System.out.println(line);  
         }  
}

完成任务.







package generconfig; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import org.springframework.context.support.GenericXmlApplicationContext; import com.pactera.service.schedule.ThreadReadPath; import com.pactera.util.CommTool; /*打包用File->Export->runnable JAR File Export Launch选择对应的类 Export destination 选择地址 Library handling:Copy required */ public class SMSThreadSender { public static void main(String[] args) { if(args.length==0) { System.out.println("短信服务开启,开始加载Spring配置。"); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false); context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); System.out.println("开整·~~~~"); CommTool.smsthreadisruning=true; ThreadReadPath thread = new ThreadReadPath(); thread.smsname="sms"+CommTool.threadid; Thread t1 = new Thread(thread); t1.setName("sms"); t1.start(); System.out.println("已经启动"); } if(args.length==1) { String pfile=args[0]; System.out.println("短信服务开启,开始加载Spring配置。"+pfile); GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.setValidating(false); context.load(pfile); // context.load("classpath:sysconfig/applicationContext.xml"); context.refresh(); CommTool.smsth
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值