开发者

Kill process by name and user

开发者 https://www.devze.com 2023-02-28 23:47 出处:网络
I am trying to figure out if there is a way to kill all processes by name and user. E.g. I want to kill all the Java instances run by user myuser.

I am trying to figure out if there is a way to kill all processes by name and user. E.g. I want to kill all the Java instances run by user myuser.

As of the moment I do:

$ pgrep -u myuser java
2185
3281
3413
3504
22534
26174
27554

which gives a list of the pid of java run by mysuer. Then I kill e开发者_开发百科ach pid individually. Is there a better way to do this?

Thanks in advance!


Use killall(1):

killall -u myuser java

Note that you may need to do this via sudo, and you may need -9 to kill processes that swallow SIGTERM:

sudo killall -9 -u myuser java


following command can do the job

$ pkill -u user process_name

note that process_name can be a regular expression also.


You're close:

$> pgrep -u myuser java | xargs kill


I know this is old but here it is an bash script to doing this easily...:

`pgrep -u root $app`
set -- $appPID
kill -9 $appPID

Here it's a blog post I wrote about this topic for killing a group of processes : https://soroo.sh/linux/kill-processess-by-name


You can do this by using

killall -u user

But sometimes this will result in a defunct process.

The best way could be like this :-

psu | grep user | awk  '{print $2}' | xargs   kill -9

NOTE :- this will also kill your current logged in session on the server if you are killing processes started by the user which you are logged on.

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号