杀死一个进程

发布于 2024-09-04 18:20:16 字数 433 浏览 4 评论 0原文

我有一个 for 循环来获取 PID 列表并杀死每个 PID。我想显示 PS 输出的整行并将其写入 /tmp/outfile 。但是从 PS 输出的每一行中,每个字段(PID,PPID,...)都与 /tmp/outfile 中的新行一起写入。因此,如果 PS 输出有三行作为输出,我想将这三行记录到 /tmp/outfile 中,但它会破坏该行中的每个字段并添加新行。我该怎么办呢。

for list in `ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep`
do
     echo "$list" >> $CUSTOM_TMP/test5566
     PID=`echo $list | awk '{print $2}'`
     kill -TERM "$list"
done

I have a for loop to get the list of PID's and kill each PID. I want to display the entire line of PS output and write it to the /tmp/outfile . But from each line of PS output each field(PID,PPID,...) is written along with a new line in the /tmp/outfile. So if PS output has three lines as output i want to log these three lines into /tmp/outfile but it's breaking each field in the line and adding a new line. how can i do it.

for list in `ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep`
do
     echo "$list" >> $CUSTOM_TMP/test5566
     PID=`echo $list | awk '{print $2}'`
     kill -TERM "$list"
done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

删除→记忆 2024-09-11 18:20:16

您的 for 循环不会迭代行,而是迭代每个单独的字段。
另外你的kill命令有点错误。
只需将您的代码更改为类似以下内容:

ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | while read list
do
     echo "$list" >> $CUSTOM_TMP/test5566
     PID=`echo $list | awk '{print $2}'`
     kill -TERM "$PID"
done

Your for loop does not iterate the lines but each individual field.
Also your kill command was slightly wrong.
Just change your code to something like:

ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | while read list
do
     echo "$list" >> $CUSTOM_TMP/test5566
     PID=`echo $list | awk '{print $2}'`
     kill -TERM "$PID"
done
唱一曲作罢 2024-09-11 18:20:16

使用 killall 命令来完成您想要做的事情不是更容易吗?

Isn't it easier to use the killall command for what you are trying to do?

渡你暖光 2024-09-11 18:20:16

根本不需要循环。这使用 tee 来编写临时文件。

list=$(ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | tee $CUSTOM_TMP/test5566 | awk '{printf "%s ", $2')
kill -TERM $list

No need for a loop at all. And this uses tee to write your temp file.

list=$(ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | tee $CUSTOM_TMP/test5566 | awk '{printf "%s ", $2')
kill -TERM $list
晒暮凉 2024-09-11 18:20:16

您想在循环之前运行 ps

ps -ef | grep $"{process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep > $CUSTOM_TMP/test5566 2>/dev/null

for PID in `cat $CUSTOM_TMP/test5566 | awk '{print $2}'`; do
      kill -TERM $PID
done
rm -f $CUSTOM_TMP/test5566

我还会插入一些理智,可能使用 wc 来确保文件实际上从 ps 获取一些数据。

You want to run ps before looping:

ps -ef | grep $"{process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep > $CUSTOM_TMP/test5566 2>/dev/null

for PID in `cat $CUSTOM_TMP/test5566 | awk '{print $2}'`; do
      kill -TERM $PID
done
rm -f $CUSTOM_TMP/test5566

I would also insert some sanity, possibly using wc to make sure the file actually got some data from ps.

执着的年纪 2024-09-11 18:20:16

只需将 awk 部分移至顶行,否则您的代码就可以了。

for list in `ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | awk '{print $2}`

do
     echo "$list" >> $CUSTOM_TMP/test5566
     PID=`echo $list`
     kill -TERM "$list"
done

Just move the awk part to the top line, otherwise your code is fine.

for list in `ps -ef | grep "${process_name}" | grep -v "${SCRIPTNAME}" | grep -v grep | awk '{print $2}`

do
     echo "$list" >> $CUSTOM_TMP/test5566
     PID=`echo $list`
     kill -TERM "$list"
done
一直在等你来 2024-09-11 18:20:16

对于单行 - 如果你的系统有 pgrep -

pgrep -d ' ' ${process_name} > kill.log && kill -TERM $(< kill.log)

For a one liner - if your system has pgrep --

pgrep -d ' ' ${process_name} > kill.log && kill -TERM $(< kill.log)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文