shell 脚本与 java 运行时不一致
shell script inconsistent with java Runtime
脚本如下
#!/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
它将 return 一条 bash
的路径 尝试使用示例 /bin/bash yourShell.sh
或 /bin/sh yourShell.sh
in Runtime#exec
或 ProcessBuilder
(推荐)
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
脚本如下
#!/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
它将 return 一条 bash
的路径 尝试使用示例 /bin/bash yourShell.sh
或 /bin/sh yourShell.sh
in Runtime#exec
或 ProcessBuilder
(推荐)
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