Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python call suppresses output until key pressed

Posted on 2013-11-26
7
Medium Priority
?
694 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 20

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 20

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 2000 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 20

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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