【发布时间】:2011-10-29 00:32:37
【问题描述】:
我编写了一个 Java 套接字服务器,以使我能够保持 Web 集群代码库同步。当我像这样从 shell 登录运行 init.d 脚本时
[root@web11 www]# /etc/init.d/servermngr start
注销后一切正常,但如果服务器重新启动或我使用类似服务运行 init.d
[root@web11 www]# service servermngr start
任何传递给套接字服务器的 exec() 命令都不会在 linux 机器上执行。我假设它与没有真正外壳的 JVM 有关。如果我登录并运行
[root@web11 www]# /etc/init.d/servermngr start
...并且注销所有运行良好所有 CVS 命令都已执行。
另一个注意事项,当作为服务运行时,套接字服务器响应状态检查,因此它正在运行
这是 init.d 脚本
#!/bin/sh
# chkconfig: 2345 95 1
# description: Starts Daemon Using ServerManager.jar.
#
# Source function library.
. /etc/init.d/functions
start () {
echo -n $"Starting ServerManager: "
# start daemon
cd /www/servermanager/
daemon java -jar ServerManager.jar > /www/logs/ServerManager.log &
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/cups
echo "";
return $RETVAL
}
stop () {
# stop daemon
echo -n $"Stopping $prog: "
kill `ps uax | grep -i "java -jar ServerManager.ja[r]" | head -n 1 | awk '{print $2}'`
RETVAL=$?
echo "";
return $RETVAL
}
restart() {
stop
start
}
case $1 in
start)
start
;;
stop)
stop
;;
*)
echo $"Usage: servermngr {start|stop}"
exit 3
esac
exit $RETVAL
以及负责实际执行代码的Java:
// Build cmd Array of Strings
String[] cmd = {"/bin/sh", "-c", "cd /www;cvs up -d htdocs/;cvs up -d phpinclude/"};
final Process process;
try {
process = Runtime.getRuntime().exec(cmd);
BufferedReader buf = new BufferedReader(new InputStreamReader(
process.getInputStream()));
// Since this is a CVS UP we return the Response to PHP
if(input.matches(".*(cvs up).*")){
String line1;
out.println("cvsupdate-start");
System.out.println("CVS Update" + input);
while ((line1 = buf.readLine()) != null) {
out.println(line1);
System.out.println("CVS:" + line1);
}
out.println("cvsupdate-end");
}
} catch (IOException ex) {
System.out.println("IOException on Run cmd " + CommandFactory.class.getName() + " " + ex);
Logger.getLogger(CommandFactory.class.getName()).log(Level.SEVERE, null, ex);
}
感谢任何帮助
【问题讨论】:
-
我检查了它写入的日志文件,它正在接收命令。它只是没有在服务器上执行它们
-
[root@web11 ~]# tail -f /www/logs/ServerManager.log 服务器开始于 ip 192.168.56.151 服务器开始于端口 4444 MaxConnections:0 CVS 更新:cd /www;cvs up -d htdocs/;cvs up -d phpinclude/ CVS 更新:cd /www;cvs up -d htdocs/;cvs up -d phpinclude/ 所以命令会到达
process = Runtime.getRuntime().exec(cmd);@Ingo
标签: java linux jar daemon runtime.exec