如何在unix中成功杀死java进程并重新启动它?
我一直在尝试整夜运行我的java程序,但有时我需要重新启动它以保存它的进度并完全重新启动机器。几个小时后,我让程序保存其进度并执行一个小的重新启动文件来重新启动程序。
void restartServer() {
try {
Runtime rt = Runtime.getRuntime();
rt.exec("./restart.bat");
} catch (java.io.IOException err) {
logError(err.getMessage());
}
}
在 restart.bat 里面我有:
echo Restarting Server
killall -9 java
sleep 2;
nohup java -Xmx200m -classpath bin server.Main;
但是它不起作用。它说:
[root@linode java]# ./restart.bat
Restarting Server
: no process killed
: command not found 3:
nohup: appending output to `nohup.out'
: command not found 4:
[root@linode java]#
当有一个java进程正在运行时,为什么它说没有进程被杀死?为什么它说命令未找到?它也永远不会重新启动程序。
I've been trying to run my java program overnight but I need to restart it sometimes to save it's progress and completely reboot the machine. After a few hours I have the program save its progress and execute a little restart file to restart the program.
void restartServer() {
try {
Runtime rt = Runtime.getRuntime();
rt.exec("./restart.bat");
} catch (java.io.IOException err) {
logError(err.getMessage());
}
}
Inside restart.bat I have:
echo Restarting Server
killall -9 java
sleep 2;
nohup java -Xmx200m -classpath bin server.Main;
However it doesn't work. It says:
[root@linode java]# ./restart.bat
Restarting Server
: no process killed
: command not found 3:
nohup: appending output to `nohup.out'
: command not found 4:
[root@linode java]#
Why does it say no process killed when there is a java process running? And why does it say command not found? It never restarts the program either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行如下脚本,而不是从服务器本身启动应用程序:
这将在进程退出后重新启动该进程。在你的保存状态退出后,它会自动重新启动。
Instead of starting the app from the server itself, you could do a script like this:
This will restart the process once it quits. And after your savestate just exit and it will restart automatically.
将
#!/bin/sh
或类似的内容作为restart.bat
的第一行怎么样?顺便说一句,
.bat
是一个非常好的 选择。 Linux 系统上的扩展选择不当。How about putting
#!/bin/sh
or something like it as the first line of yourrestart.bat
BTW,
.bat
is a very poor choice of extension on linux system.像中间进程那样杀死你的 JVM 安全吗?如果没有,您可能会考虑使用 JMX bean 来检查程序状态并从 JVM 内部进行关闭。
is it safe for your JVM to be killed like that mid process? If not, you might consider a JMX bean to check the program status and do the shutdown from inside the JVM.