[英]Grepping using shell - long delay
我有一个基准线程正在运行,并且需要几个小时才能运行。 启动基准线程的脚本是使用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时立即打印该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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.