简体   繁体   中英

Bash - How to kill all the processes of the current user that meet a criteria?

I am currently using ps -o pid,cmd | awk '{if($2=="watch") print $1}' | xargs kill -1 ps -o pid,cmd | awk '{if($2=="watch") print $1}' | xargs kill -1 ps -o pid,cmd | awk '{if($2=="watch") print $1}' | xargs kill -1 .

I would like to expand the command to kill the processes of the current user.

What I got working is ps -o uid,pid,cmd | awk '{if($1==1000 && $3=="watch") print $2}' | xargs kill -1 ps -o uid,pid,cmd | awk '{if($1==1000 && $3=="watch") print $2}' | xargs kill -1 ps -o uid,pid,cmd | awk '{if($1==1000 && $3=="watch") print $2}' | xargs kill -1 but I would like to replace $1==1000 with something like id -u but its not working.

You can use:

ps -o uid,pid,cmd |
awk -v uid=$(id -u) '$1==uid && $3=="watch"{print $2}' |
xargs kill -1

I would suggest a simpler command like

pkill -U $USER watch

Note that you can target more than one user by using -U user1,user2,...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM