我有一个基准线程在运行,它需要几个小时才能运行。启动基准线程的脚本是使用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。
你看到什么问题了吗?或者我如何才能纠正这一点?
发布于 2013-02-05 05:19:40
这句话
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 tmpfilehttps://stackoverflow.com/questions/14695559
复制相似问题