Solved

Python call suppresses output until key pressed

Posted on 2013-11-26
7
601 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 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWK How do I reorder the columns in a csv 7 58
SSL/TLS - openssl troubleshooting 3 58
plsql job on oracle 18 72
Certificate Request CentOS/Apache 1 17
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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