bash 如何在bash中快速杀死java进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15508307/
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 quickly kill java processes in bash?
提问by iCodeLikeImDrunk
On a linux box, I have at most 3 java jars files running. How do I quickly kill all 3 with one command?
在 linux 机器上,我最多运行 3 个 java jars 文件。如何用一个命令快速杀死所有 3 个?
Usually I would:
通常我会:
ps ex - get the processes running
ps ex - 让进程运行
then find the process ids then do:
然后找到进程ID然后执行:
kill -9 #### #### ####
kill -9 #### #### ####
Any way to shorten this process? My eyes hurts from squinting to find the process ids.
有什么办法可以缩短这个过程?我的眼睛因为眯着眼睛寻找进程ID而受伤。
My script does the following:
我的脚本执行以下操作:
nohup ./start-gossip &
nohup ./start &
nohup ./start-admin &
Is there a way to get the process ids of each without looking it up?
有没有办法在不查找的情况下获取每个进程的 ID?
采纳答案by davmac
You can save the PIDs when you start the processes so you can use them later:
您可以在启动进程时保存 PID,以便以后使用它们:
nohup ./start-gossip &
START_GOSSIP_PID=$!
nohup ./start &
START_PID=$!
nohup ./start-admin &
START_ADMIN_PID=$!
...
kill -9 $START_GOSSIP_PID
kill -9 $START_PID
kill -9 $START_ADMIN_PID
This has the advantage (over pkill
) of not killing off any other processes that coincidentally have similar names. If you don't want to perform the kill operation from the script itself, but just want to have the PIDs handy, write them to a file (from the script):
这具有(超过pkill
)的优点,即不会杀死任何其他巧合具有相似名称的进程。如果您不想从脚本本身执行终止操作,而只想方便地使用 PID,请将它们写入文件(从脚本):
echo $START_GOSSIP_PID > /some/path/start_gossip.pid
Or even just do this when you launch the process, rather than saving the PID to a variable:
或者甚至只是在启动进程时执行此操作,而不是将 PID 保存到变量:
nohup ./start-gossip &
echo $! > /some/path/start_gossip.pid
回答by FatalError
Short answer:
简短的回答:
pkill java
This looks up a process (or processes) to kill by name. This will find any other java
processes too, so be careful. It also accepts -9
, but you should avoid using -9
unless something is reallybroken.
这会按名称查找要杀死的进程(或多个进程)。这也会找到任何其他java
进程,所以要小心。它也接受-9
,但-9
除非某些东西真的坏了,否则你应该避免使用。
EDIT:
编辑:
Based on updates, you may be able to specify the script names to pkill
as well (I'm not positive). But, the more traditional way to handle this issue is to leave pid files around. After you start a new process and background it, its pid is available in $!
. If you write that pid to a file, then it's easy to check if the process is still running and kill just the processes you mean to. There is some chance that the pid will be reused, however.
根据更新,您也可以指定脚本名称pkill
(我不肯定)。但是,处理这个问题的更传统的方法是留下 pid 文件。在您启动一个新进程并将其后台运行后,它的 pid 可以在$!
. 如果您将该 pid 写入文件,则很容易检查该进程是否仍在运行并仅终止您想要的进程。然而,pid 有可能被重用。
回答by Pravin Netke
To get the process id of that java process run
获取该java进程运行的进程ID
netstat -tuplen
netstat -元组
Process ID (PID) of that process whom you want to kill and run
您要杀死并运行的那个进程的进程 ID (PID)
kill -9 PID
杀死 -9 PID