linux - 从 pgrep grunt 中杀死 PID 的 bash 脚本

标签 linux bash shell

我是 bash 脚本的全明星菜鸟。我的脚本看起来像这样:

#!/bin/bash 

pgrep grunt > target.txt  
for i in target.txt  
   do kill $i
   echo "killed $i"  
done

现在,显然它没有用,我不知道如何让它工作。 我希望此脚本将 pgrep grunt 的输出echo 到文件 target.txt 并从那里读取 PID 并终止这些进程。

最佳答案

回答你的问题:

#!/bin/bash

while read i; do 
   kill $i
   echo "killed $i"
done< <(pgrep grunt)

不需要外部文件

但是,你知道 pkill 的存在吗?

 pkill grunt

pgrep, pkill - look up or signal processes based on name and other attributes

关于linux - 从 pgrep grunt 中杀死 PID 的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42696805/

相关文章:

linux - 可以在 Windows 上的 Docker 中运行 ARM/rpi 图像,但不能在 linux 上运行

linux - 列与制表符而不是空格对齐

ruby - 是否可以从 ruby​​ 脚本导出环境属性?

Python:对同一个程序使用多个Python窗口

c++ - 将 NULL 指针访问转换为 Linux/GCC 下的 C++ 异常

c - MS office解密

bash - 为什么 bash 的流控制结构不一致?

bash - Cygwin 'less' 命令使 bash 忘记屏幕缓冲区历史记录

linux - 删除除最后 5 个文件夹之外的所有文件夹的 Shell 脚本

shell - shell 脚本中的命令替换,无需通配符