Linux 使用shell脚本自动杀死进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11389543/
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
Using a shell script to automatically kill processes
提问by verystrongjoe
I am looking for a command that will find PIDs such as:
我正在寻找一个可以找到 PID 的命令,例如:
ps -ef | grep com.sds.afi.rte.cosmos-1.0.0.jar
cosmos 4690 4689 3 14:27 pts/8 00:00:06 java -Dlog4j.debug -Dlog4j.configuration=file:/data/cosmos/sim/bin/log4j.xml -jar com.sds.afi.rte.cosmos-1.0.0.jar
cosmos 5484 5482 0 14:30 pts/11 00:00:00 grep com.sds.afi.rte.cosmos-1.0.0.jar
and then kill these processes using:
然后使用以下命令终止这些进程:
kill -9 pid
How can I make a shell script that will do all the above automatically ?
如何制作一个可以自动完成上述所有操作的 shell 脚本?
回答by Mark Bramnik
Try this:
尝试这个:
kill -9 `pidof java`
回答by ErJab
killall java
killall java
Or more generically:
或更笼统地说:
killall <processname>
killall <processname>
Sometimes I have processes with the same name, but different command line arguments. To kill such processes or any arbitrary process without having to type in ps and then kill pid, I do this:
有时我的进程名称相同,但命令行参数不同。要终止此类进程或任何任意进程而不必输入 ps 然后终止 pid,我这样做:
ps aux | grep <something> | awk '{print $2}' | xargs kill
ps aux | grep <something> | awk '{print $2}' | xargs kill
where
在哪里
<something>
is any phrase that you want to search for in the ps aux command's output.awk '{print $2}'
will filter out only the 2nd column, which is a list of PIDskill
will be called on each of those PIDs.
<something>
是您要在 ps aux 命令的输出中搜索的任何短语。awk '{print $2}'
将仅过滤掉第二列,即 PID 列表kill
将在每个 PID 上调用。
Edit: As tripleee points out, it is a bad idea to lash out kill -9
on a process unless absolutely needed. So removed the -9
part from the above command
编辑:正如三重奏指出的那样,kill -9
除非绝对需要,否则对流程进行猛烈抨击是一个坏主意。所以-9
从上面的命令中删除了部分
回答by nikeairj
I think pkill -9 java
is the easiest way. pkill
will use grep to find a matching process name.
我认为pkill -9 java
是最简单的方法。pkill
将使用 grep 查找匹配的进程名称。
See the manual page: http://linux.die.net/man/1/pkill
参见手册页:http: //linux.die.net/man/1/pkill
回答by Sjors
In addition to the solution ErJabprovided, I created a shell script killall to mimic the 'killall' behaviour:
除了ErJab提供的解决方案之外,我还创建了一个 shell 脚本 killall 来模仿“killall”行为:
#!/bin/sh
ps | grep | awk '{print }' | xargs kill -9
Which I placed in the /bin directory (after chmod +x, of course)
我放在 /bin 目录中(当然是在 chmod +x 之后)
回答by Andreas Dietrich
I find the pkill
/pgrep
commands as mentioned by nikeairj
the best choice if available.
I also used the following which may also work in Linux/Unix OSes without awk
or other possibly unavailable commands(I remember not beeing able to use awk
in some AIX
or HP-UX
environments)
如果可用,我会找到最佳选择中提到的pkill
/pgrep
命令nikeairj
。我还使用了以下也可以在没有awk
或其他可能不可用的命令的Linux/Unix 操作系统中工作(我记得不能awk
在某些AIX
或HP-UX
环境中使用)
# subsitute myMatch with your process cmdline match, e.g. "firefox", "firefox -P"
# or anything output by "ps -ef"
ps -ef|grep myMatch|grep -v grep|sed -e "s/^[^0-9]\+\([0-9]\+\)\s.\+$//"|xargs kill "{}"
回答by Suraj Branwal
You can include below command in your shell script which will kill the process id for "com.sds.afi.rte.cosmos-1.0.0.jar"
您可以在 shell 脚本中包含以下命令,该命令将终止“com.sds.afi.rte.cosmos-1.0.0.jar”的进程 ID
kill -9 ps aux | grep com.sds.afi.rte.cosmos-1.0.0.jar | grep -v grep | awk '{print \$2}'
杀 -9 ps aux | grep com.sds.afi.rte.cosmos-1.0.0.jar | grep -v grep | awk '{print \$2}'
make sure you use inverted (``) comma for pid search as used above.
确保使用倒置的 (``) 逗号进行 pid 搜索,如上所述。
if you want to execute the above command from expect command then you can use below:
如果要从 expect 命令执行上述命令,则可以使用以下命令:
expect "$"
期待“$”
send "kill -9 ps aux | grep com.sds.afi.rte.cosmos-1.0.0.jar | grep -v grep | awk '{print \$2}'
\r"
发送“kill -9 ps aux | grep com.sds.afi.rte.cosmos-1.0.0.jar | grep -v grep | awk '{print \$2}'
\r”
Cheers, Suraj
干杯,苏拉杰
回答by Andrew Haraldstad
I made a .sh to restart my Wifi after putting it in monitor mode with the following lines.
在使用以下几行将其置于监控模式后,我制作了一个 .sh 以重新启动我的 Wifi。
#Display PID
echo "Killing network PID'S"
ps aux | grep wpa_supplicant | awk '{print }' | xargs kill
ps aux | grep NetworkManager | awk '{print }' | xargs kill
#Restart NetworkManager && wpa_supplicant
echo "Restarting NetworkManager && wpa_supplicant"
service NetworkManager restart && service wpa_supplicant restart