Starting and killing java app with shell script (Debian)(使用 shell 脚本启动和终止 java 应用程序(Debian))
问题描述
我是 UNIX 新手.我想用这样的脚本启动我的 java 应用程序:
I'm new to UNIX. I want to start my java app with a script like so:
#!/bin/sh
java -jar /usr/ScriptCheck.jar &
echo $! > /var/run/ScriptCheck.pid
这应该是有效的.它确实运行应用程序并且确实写入了 pid 文件.但是当我尝试使用包含以下内容的不同脚本停止该过程时:
This is supposedly working. It does run the app and it does write the pid file. But when I try to stop the process with a different script which contains this:
#!/bin/sh
kill -9 /var/run/ScriptCheck.pid
控制台给了我这个错误:
the console gives me this error:
bash: kill: /var/run/ScriptCheck.pid: arguments must be process or job IDs
我最好的猜测是我没有在停止脚本中编写正确的代码,可能没有给出正确的命令来打开 .pid 文件.任何帮助将不胜感激.
My best guess is that I'm not writing the right code in the stop script, maybe not giving the right command to open the .pid file. Any help will be very appreciated.
推荐答案
当 kill
需要一个 (proces id) 编号时,您将文件名作为参数传递给它,所以只需阅读来自该文件的进程 ID 并将其传递给 kill
:
You're passing a file name as an argument to kill
when it expects a (proces id) number, so just read the process id from that file and pass it to kill
:
#!/bin/sh
PID=$(cat /var/run/ScriptCheck.pid)
kill -9 $PID
这篇关于使用 shell 脚本启动和终止 java 应用程序(Debian)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 shell 脚本启动和终止 java 应用程序(Debian)


基础教程推荐
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01