pistal
pistal

Reputation: 2456

Grepping using shell - long delay

I have a benchmark thread running and it takes a couple of hours to run. The script for initiating the benchmark thread was done using python. It prints out some random "foo" and I want to grep it for further use.

So, I wrote a shell script that does this.

#!/bin/bash

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

Since, the thread takes a very long time. Maybe the shell script is unable to jump to the next line till the execution is over and I am unable to print the id as soon as it initiates it..

do you see any problem? or how I can rectify this?

Upvotes: 0

Views: 307

Answers (1)

chepner
chepner

Reputation: 531718

This statement

echo $id

cannot run until the previous statement

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

completes. If you don't need $id, get rid of it and simply run

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

to see the output as it is generated (but you may need to disable buffering, as pointed out by Martijn). If you do need $id, you can use the tee command to store a copy of the output and print it to standard error at the same time:

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

A third option is to use a temporary file.

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

Upvotes: 1

Related Questions