Bash - 如何杀死当前用户所有符合条件的进程?

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

我目前正在使用 ps -o pid,cmd | awk '{if(=="watch") print }' | xargs kill -1

我想扩展命令来杀死当前用户的进程。

我得到的工作是 ps -o uid,pid,cmd | awk '{if(==1000 && =="watch") print }' | xargs kill -1 但我想用 id -u 之类的东西替换 $1==1000 但它不起作用。

您可以使用:

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

我建议使用更简单的命令,例如

pkill -U $USER watch

请注意,您可以使用 -U user1,user2,...

定位多个用户