脚本如下。
#!/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 文件有类似的建议吗?
最佳答案
检查文件权限是否有执行权限,如果没有则设置执行权限。
执行 which bash
它将返回 bash
的路径 尝试使用示例 /bin/bash yourShell.sh
或 /在
(推荐)Runtime#exec
或 ProcessBuilder
中 bin/sh yourShell.sh
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
关于java - shell脚本与java运行时不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33116403/