Link to home
Start Free TrialLog in
Avatar of simon3270
simon3270Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Python call suppresses output until key pressed

I have a loop in a Bash shell script that repeatedly calls a Python script (here, "checkservice"), and exits when the Python script produces a particular output string:
        while true; do
                sleep 1
                time_elapsed $start

                v=$(/usr/bin/checkservice /show | grep "process.*boot.*Applied")
                if [ "$v" != "" ]; then
                    printf "\n\n"
                    break
                fi
        done
        echo "Process found"

Open in new window

The same loop construct is used elsewhere, but with different commands (e.g. a "ps" to wait for another process to stop).  The time_elapsed function displays the time since the start of the loop.

In most instances of this loop, it successfully displays the time until the correct pattern is printed out, then displays the "Process found" line and carries on with the rest of the script, displaying text as it goes.  With this particular version, the time display starts incrementing, but around the time that the text condition is met, the display just hangs.  The script itself carries on, it's just that there is no output.  If I press a key (e.g. the space bar), the output stream suddenly comes back to life, displaying text from the last few passes through the loop, and all of the text that came after the loop.

I can't post the contents of the "checkservice" command (it's proprietary, and enormous), but I am asking what sort of thing I should be looking for to see why the output stops.  I've tried it with a cut-down Python script, and it doesn't hang in the same way, so there must be something in the main code which causes this.  The Python script is not interactive - it just takes its arguments, and produces data based on the particular arguments.

Alternatively, is there any way i can protect the shell script from this behaviour?  I've tried redirecting the input to the Python code (e.g. by adding "echo |" in front of it, and by adding "</dev/null" after it).  The code used to have the "checkservice" command running as a simple command with a grep after it, and the code would check the return code from the grep, but that hangs too.
Avatar of pepr
pepr

I guess you have access to the Python source, and you can possibly it if neccessary.

What is the command that produces the output on display? It seems that you may want to call .flush() method of the output stream.
Avatar of simon3270

ASKER

I do have the source, but I'd rather not modify it (it's already in production systems, and changing it just to get an installation monitoring script working might not be acceptable!)

The Python code is only producing output within the v=$(...) construct - it always produces lots of output (500 lines or more), but the grep brings it down to 0 or 1 lines.  It will be 0 lines for several minutes, then be 1 line when the particular component I am interested in is installed.  As soon as grep produces that single line, the loop is supposed to terminate and the script then carries on with the rest of its work.

The problem appears to be in the "print" and "echo" output - it is *that* which stops, around the time that the grep produces its single line.  Oddly, it doesn't even produce output if the above is the last part of the script, and it just returns to the shell prompt - that prompt only appears if I hit a key.
Try to put echo between the lines 5 and 6 to discover whether the line 5 causes the behaviour, or some other line.

Also, the substitution at the line 5 removes trailing newlines. Try to echo $v.

I have seen somewhere that printf "\n" does make new line. But I am not using shell script regularly. Try printf "\nxxxx\n" or empty echo instead.
Hmm, that "echo" seems to help on its own"  One thing I forgot to mention is that the time_elapsed call does
    printf "\rTime %02d:%02d%40s" "$mins" "$secs" " "
to overwrite the time on the same line each time as it rises.  I will try it out with a different "overwriting" method (it just looks messy if the time is written to a new line every second).
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That sorted it - I put stdbuf -o0 on the Python script, and it didn't hang (the subsequent output was displayed immediately.  thanks!
I thank you for teaching me via searching the answer ;)