python - 使用 shell 进行 Grepping - 长时间延迟

标签 python linux shell

我有一个基准线程正在运行,它需要几个小时才能运行。 启动基准线程的脚本是使用 python 完成的。 它打印出一些随机的“foo”,我想 grep 它以供进一步使用。

因此,我编写了一个执行此操作的 shell 脚本。

#!/bin/bash

id = `taskset -c 0 python <path>/run-apps.py <thread> | grep "pid" | awk '{print $2}'`
echo $id

因为,线程需要很长时间。 也许 shell 脚本在执行结束之前无法跳转到下一行,并且我无法在它启动后立即打印 id..

你看到什么问题了吗?或者我该如何纠正这个问题?

最佳答案

这个声明

echo $id

直到上一个语句才能运行

id=`taskset -c 0 python <path>/run-apps.py <thread> | grep "pid" | awk '{print $2}'`

完成。如果你不需要 $id,去掉它,简单地运行

taskset -c 0 python <path>/run-apps.py <thread> | grep "pid" | awk '{print $2}'

查看生成的输出(但您可能需要禁用缓冲,正如 Martijn 所指出的)。如果确实需要 $id,可以使用 tee 命令 存储输出的副本并同时将其打印到标准错误:

id=$(taskset -c 0 python <path>/run-apps.py <thread> |\
     grep "pid" | awk '{print $2}' | tee /dev/stderr) # Or some other file descriptor that goes to your terminal

第三种选择是使用临时文件。

taskset -c 0 python <path>/run-apps.py <thread> | grep "pid" | awk '{print $2}' > tmpfile &
tail --pid $! -f tmpfile # Watch tmpfile until the backgrounded job completes
do-other-job --reading-from tmpfile

关于python - 使用 shell 进行 Grepping - 长时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14695559/

相关文章:

linux - 命令行参数中的 "-"(破折号)有什么魔力?

python - 如何提高 Pycharm 文档获取的性能

python - 带有 Python 的 Google 应用引擎 : Unable to update the data entity

c - Linux C 中的多线程队列

Bash 无意中分割命令

python - 如何在 Linux 中重启 IDLE Python Shell?

python - 允许用户删除 python 中的用户输入

python - 在 pandas 数据框中第一次出现条件后删除所有行

c++ - 我目前正在使用什么 c++ norme?

linux - 如何在没有大堆栈的情况下在 2 个不同的 Centos 主机上建立 2 个服务之间的依赖关系?