Solved

Python call suppresses output until key pressed

Posted on 2013-11-26
7
573 Views
Last Modified: 2013-11-27
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.
0
Comment
Question by:simon3270
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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.
0
 
LVL 19

Author Comment

by:simon3270
Comment Utility
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.
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 19

Author Comment

by:simon3270
Comment Utility
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).
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
Comment Utility
It is apparently related to flushing. I do not know how it should be done. You may find some answer in http://www.pixelbeat.org/programming/stdio_buffering/ or in the like articles.
0
 
LVL 19

Author Comment

by:simon3270
Comment Utility
That sorted it - I put stdbuf -o0 on the Python script, and it didn't hang (the subsequent output was displayed immediately.  thanks!
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
I thank you for teaching me via searching the answer ;)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now