我有一个基准线程正在运行,它需要几个小时才能运行。 启动基准线程的脚本是使用 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/