杀死一个进程
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的 for 循环不会迭代行,而是迭代每个单独的字段。
另外你的kill命令有点错误。
只需将您的代码更改为类似以下内容:
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:
使用 killall 命令来完成您想要做的事情不是更容易吗?
Isn't it easier to use the killall command for what you are trying to do?
根本不需要循环。这使用
tee
来编写临时文件。No need for a loop at all. And this uses
tee
to write your temp file.您想在循环之前运行
ps
:我还会插入一些理智,可能使用
wc
来确保文件实际上从ps
获取一些数据。You want to run
ps
before looping:I would also insert some sanity, possibly using
wc
to make sure the file actually got some data fromps
.只需将 awk 部分移至顶行,否则您的代码就可以了。
Just move the awk part to the top line, otherwise your code is fine.
对于单行 - 如果你的系统有 pgrep -
For a one liner - if your system has pgrep --