如何从命令行停止 Java 程序的执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3014105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop the execution of Java program from Command line?
提问by Aakash
My main field is .Net but recently I have got something to do with Java. I have to create a shell utility in Java that could run in background reading few database records after specified duration and do further processing. It's a kind of scheduler. Now I have few concerns:
我的主要领域是 .Net 但最近我与 Java 有关。我必须在 Java 中创建一个 shell 实用程序,它可以在后台运行,在指定的持续时间后读取少量数据库记录并进行进一步处理。这是一种调度程序。现在我有几个顾虑:
How to make this work as a service. I want to execute it through a shell script and the utility should start running. Off course the control should get back to the calling script.
如何使这项工作成为一项服务。我想通过 shell 脚本执行它,该实用程序应该开始运行。当然,控件应该返回到调用脚本。
Secondly, eventually i may want to stop this process from running. How to achieve this?
其次,最终我可能想阻止这个进程运行。如何实现这一目标?
I understand these are basic question but I really have no idea where to begin and what options are best for me.
我知道这些是基本问题,但我真的不知道从哪里开始以及哪些选项最适合我。
Any help / advise please?
请问有什么帮助/建议吗?
采纳答案by Luhar
I would go for the running the program using a scheduler or a service. However, if you wish to use a bat file and do this programmatically, I have outlined a possible approach below:
我会使用调度程序或服务来运行程序。但是,如果您希望使用 bat 文件并以编程方式执行此操作,我在下面概述了一种可能的方法:
In your Java program, you can get the PID programmatically, and then write it to a file:
在您的 Java 程序中,您可以通过编程方式获取 PID,然后将其写入文件:
public static void writePID(String fileLocation) throws IOException
{
// Use the engine management bean in java to find out the pid
// and to write to a file
if (fileLocation.length() == 0)
{
fileLocation = DEFAULT_PID_FILE;
}
String pid = ManagementFactory.getRuntimeMXBean().getName();
if (pid.indexOf("@") != -1)
{
pid = pid.substring(0, pid.indexOf("@"));
}
BufferedWriter writer = new BufferedWriter(new FileWriter(fileLocation));
writer.write(pid);
writer.newLine();
writer.flush();
writer.close();
}
You can then write a stop .bat file that will kill the running program in windows. You could do something like:
然后,您可以编写一个 stop .bat 文件,该文件将终止 Windows 中正在运行的程序。你可以这样做:
setlocal
IF EXIST app.pid FOR /F %%i in ('type app.pid') do TASKKILL /F /PID %%i
IF EXIST app.pid DEL app.pid
endlocal
Of course, app.pid is the file written by the Java method above.
当然app.pid就是上面Java方法写的文件。
I am not sure how you would be able to write a script that launches a java program, and reverts control on termination. I would be interested to see if anybody has a solution for that.
我不确定您将如何编写启动 Java 程序并在终止时恢复控制的脚本。我很想知道是否有人对此有解决方案。
回答by Bozho
ps ux
see pid
看 pid
kill pid
Or you'd better provide a stopping script that signals the application, which does System.exit(0)
或者你最好提供一个停止脚本来向应用程序发出信号,它不会 System.exit(0)
回答by bshields
You didn't specify the platform. If on Windows you should look into integrating with the Service Control to create a Windows service. http://en.wikipedia.org/wiki/Windows_service. Once you've implemented the service hooks, it is possible to start and stop the service through the service control GUI or using net stop MyService
syntax from the command line.
你没有指定平台。如果在 Windows 上,您应该考虑与服务控制集成以创建 Windows 服务。 http://en.wikipedia.org/wiki/Windows_service。一旦您实现了服务挂钩,就可以通过服务控制 GUI 或使用net stop MyService
命令行中的语法来启动和停止服务。
回答by Kent
I assume that you are playing your java program with a Linux/Unix box. To run your application as a daemon, you can try
我假设您正在使用 Linux/Unix 机器播放 Java 程序。要将您的应用程序作为守护程序运行,您可以尝试
nohup java YourJavaClass &
To stop your application, you can either:
要停止您的应用程序,您可以:
kill [psIdofYourApplication]
or
或者
fg [your application job Id]
Ctrl-C
If you want to do some postprocessing after the application receiving 'kill/stop' signal. check out addShutdownHook(Thread hook)
如果您想在应用程序收到“终止/停止”信号后进行一些后处理。查看addShutdownHook(Thread hook)
Or sun.misc.SignalHandler
或者 sun.misc.SignalHandler
回答by Pavel Vyazankin
As I understand, you want something like this:
据我了解,您想要这样的东西:
if ( System.in.avaliable() > 0 ) {
in = new BufferedReader( new InputStreamReader( System.in );
String InLine = in.readLine();
...
}
Am I right?
我对吗?