【问题标题】:shell script inconsistent with java Runtimeshell脚本与java Runtime不一致
【发布时间】:2015-10-14 04:01:51
【问题描述】:

下面是脚本。

#!/bin/bash
if [ ! -s $CATALINA_HOME ]; then
echo "Error: CATALINA_HOME not set!!!"
exit -1
fi

SHUTDOWN_SCRIPT="$CATALINA_HOME/bin/shutdown.sh"
STARTUP_SCRIPT="$CATALINA_HOME/bin/startup.sh"

#execute shutdown.sh
echo "Doing Shutdown"
$SHUTDOWN_SCRIPT
sleep 5
#execute startup.sh
echo "Doing Startup"
$STARTUP_SCRIPT

exit 0

它只是关闭,启动不工作。

我正在使用以下代码运行

Runtime rn = Runtime.getRuntime();
Process proc; 
proc = rn.exec("/script/restart.sh");
proc.waitFor(); 

我对 .bat 文件有类似的问题,我用

修复了它
Runtime rn = Runtime.getRuntime();
Process proc; 
proc = rn.exec("cmd /c start /script/restart.bat"); 
proc.waitFor();

用“cmd /c start”调用解决了我的问题,对 .sh 文件有什么类似的建议吗?

【问题讨论】:

    标签: java linux shell batch-file runtime


    【解决方案1】:

    检查文件权限是否有执行权限,如果没有设置执行权限。

    执行which bash 它将返回bash 的路径尝试在Runtime#execProcessBuilder 中使用示例/bin/bash yourShell.sh/bin/sh yourShell.shProcessBuilder(推荐)

    saravana@ubuntu:~$which bash /bin/bash

        String[] commmands = { "/bin/bash", "/script/restart.sh" }; // or { "/bin/sh", "/script/restart.sh" }
        Runtime.getRuntime().exec(commmands); //or
        new ProcessBuilder(commmands).start(); //Recommended
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      相关资源
      最近更新 更多