Solved

Python call suppresses output until key pressed

Posted on 2013-11-26
7
628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:pepr
ID: 39680070
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
ID: 39680105
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 29

Expert Comment

by:pepr
ID: 39680456
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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 19

Author Comment

by:simon3270
ID: 39680619
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 29

Accepted Solution

by:
pepr earned 500 total points
ID: 39680817
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
ID: 39680877
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 29

Expert Comment

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

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

617 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