5

This may be a super simple question. I am calling a Python script on an Lubuntu OS (Cubietruck) via a shell script. This is to automate the process on start up (I want to maintain this process). Is there a simple way to view the output from Python? At the moment the Python script runs in the background, with no terminal. I have some errors that need checking. The script is:

#!/bin/sh
python recordSound.py

Thanks in advance.

JJC
  • 68
  • 1
  • 1
  • 3
  • Does `python recordSound.py` produce any output when you run it on the command line directly? – Miquel Jul 10 '14 at 09:37
  • 3
    Add [`logging`](https://docs.python.org/2/library/logging.html) to your script, and then read the logs. – Burhan Khalid Jul 10 '14 at 09:41
  • Hi Miquel, Yes it does. I have a number of print statements / exceptions to catch progress and errors. – JJC Jul 10 '14 at 09:43
  • Hi Burhan, I thought of logging output but wanted to check things in real time if possible as this a script that runs at start up and triggers other applications plus hardware. It also will be a headless unit that I will VNCing into at the beginning so I want to see progress early on. Thanks for the suggestion though. It may be the way forward. – JJC Jul 10 '14 at 09:46

5 Answers5

8

The proper solution would probably use logging as proposed in a comment.

In the meantime, you have to redirect both the standard output and the standard error stream in order to capture the normal output as wall as any error reported by your script:

#!/bin/sh
python recordSound.py >> logfile.log 2&>1

See one of the many web pages on that topic to explore the various redirection available from a shell script.


In addition, if you need both login and live view on the console, use the tee standard command:

#!/bin/sh
python recordSound.py 2&>1 | tee logfile.log
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125
2

Call your script directly. Just like you call bash script with a shebang (#!) you can call your python script with a #!/bin/env python. Then all you need to do is giving executable permission via chmod +x yourscript.py and calling your script directly like an application. This way, you can see the error codes. Another way to do is using logger module, which is great to see tracebacks and debug info.

Benj
  • 736
  • 7
  • 21
Cediddi
  • 130
  • 1
  • 8
  • I have tried this, but with no joy. It may be my PATH. The general consensus is to go for the logger approach. – JJC Jul 10 '14 at 13:04
1

In your python code you may need to add flush=True to get the print message visible immediately when run thru .sh

print('your message', flush=True)

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
0

I'm no bash expert, but I think this should work, as python prints on the standart output unless told otherwise :

#!/bin/sh
python recordSound.py >> /tmp/my_python_log.txt

will add lines each time python outputs stuff. Go to the path specified to read the result. If you want a live view, use pipes '|' to redirect the output.

Edit : for a live view, you can also use the "tail -f /tmp/my_python_log.txt" command on another terminal as the other solution is running, which will first print the last 10 lines or so in the file, then automatically add new lines added. I love this command, really handy to view activity on a small Apache server. You can even watch multiple logs at once !

PhilDenfer
  • 270
  • 4
  • 13
  • After reading Burhan's comment, I'd say his solution is more robust and efficient / right way of doing things, but for a small script, manual redirection, the lazy way, using bash to simplify everything even if it looks pointless to print via python then redirect when you can directly log from python, is better because you can easily go from "logging" to "live output" by removing the '|' or '>>'. – PhilDenfer Jul 10 '14 at 09:48
  • But don't forget that logging can produce standard output or error output if so configured ... – holdenweb Jul 10 '14 at 09:52
  • Yes, but it requires to read its manual, I often go for simple generic non-efficient solutions because I know for sure how they behave in each and every situation. I might slightly be reinventing the wheel but at least my wheel won't randomly throw an exception I don't know about at the 500th execution. – PhilDenfer Jul 10 '14 at 09:58
  • 1
    Fair enough - I too try to follow the KISS principle – holdenweb Jul 10 '14 at 10:10
0

A very easy way would to redirect output from your script to a file

#!/bin/sh
python recordSound.py >> logfile.log

See the docs on IO redirection

Eero Aaltonen
  • 4,239
  • 1
  • 29
  • 41