2

I have a issue with my raspberry pi that starts up a python script it generates a exception somewhere and I have catched all errors which are printed into the terminal (if running from idle).

But how do I save the printed output to a file when it is running on boot? I found this script below on the internet but it doesn't seem to write the printed text,it creates the file but the content is empty (every 30 sec it should print 'Checking')

This is the code that is being executed after the pi has booted and it shows the GUI elements perfectly (and yes sudo is required due to the GPIO pins)

#!/bin/sh
startx
sudo python /home/pi/python_gui.py > /home/pi/output.log

Hopefully someone knows a answer, thanks in advance!

crasyboy42
  • 57
  • 2
  • 7
  • 1
    what's in your python_gui.py code? Does it do any output? – Ashalynd Jan 15 '16 at 14:09
  • It prints every 30 sec 'Checking' which is done through tkinter loop. and when it catches a exception but nothing else – crasyboy42 Jan 15 '16 at 14:11
  • On your script add 'echo date +"%T" >> /home/pi/output.log' to confirm your job is running, Maybe before and after every statement. – fcm Feb 28 '17 at 13:20

1 Answers1

1

Without seeing the errors that are being output, it will be very difficult to debug any failing script. So, first you should ensure that you are logging the exception. The line:

sudo python /home/pi/python_gui.py > /home/pi/output.log

will redirect stdout to the file /home/pi/output.log. However, if you want to log errors you also need to redirect stderr. You have two choices here, either redirecting stderr to a different file, or alternatively redirecting it to the same file, by redirecting it to stdout. For example:

To redirect to a new file:

sudo python /home/pi/python_gui.py > /home/pi/output.log 2> /home/pi/error.log

To redirect both to the same file:

sudo python /home/pi/python_gui.py &> /home/pi/output.log

Or:

sudo python /home/pi/python_gui.py > /home/pi/output.log 2>&1

You can find more information and examples on this question.

If you want to update the logfile each time you run, you can append to it using >> instead, e.g.:

sudo python /home/pi/python_gui.py &>> /home/pi/output.log
Community
  • 1
  • 1
mfitzp
  • 15,275
  • 7
  • 50
  • 70
  • Thanks for your comment, I tried sudo python /home/pi/python_gui.py &> /home/pi/output.log and sudo python /home/pi/python_gui.py > /home/pi/output.log 2> /home/pi/error.log, both are not working the log file is still empty (no printed text to be found) – crasyboy42 Jan 15 '16 at 14:43
  • Are you sure the script is being run? Can you post the content of the script? – mfitzp Jan 15 '16 at 14:44
  • Did the second one create the `/home/pi/error.log` file? – mfitzp Jan 15 '16 at 14:49
  • The content of the python script or the .sh script? and i am sure it is being run since i see the GUI startup :) The error.log is indeed being created yes, its there just empty in leafpad. – crasyboy42 Jan 15 '16 at 14:53
  • The python script if you can. If those files are being created it sounds like its running but failing early (without throwing an error). – mfitzp Jan 15 '16 at 14:56
  • pastebin.com/xK1CMssa that is the main code with the try catch on the only part where it COULD crash, its weird that it should crash because it gives no errors in idle on boot, and gui is running perfectly after too – crasyboy42 Jan 15 '16 at 15:04
  • Thanks, that's great. I notice you're using Tk... Is there a window manager available on your Pi (at the time this script is running)? – mfitzp Jan 15 '16 at 15:08
  • Yes there is, also on remote through putty and xming its also running and both are working fine and display also works correctly (adafruit pitft 480x320), I can not interact with anything on the pi tft since it is fullscreen app that is running, only through xming fyi. – crasyboy42 Jan 15 '16 at 15:12
  • Well that is strange. Can you get *any* output from the script to the log file (try more print statements, or using `logging`)? – mfitzp Jan 15 '16 at 15:34
  • I will try logging when i am back from work later today , but so far it is empty,all of it sadly – crasyboy42 Jan 15 '16 at 15:38
  • It seems that the original comment of you worked, it just took half an hour until it started printin in that file, I just have one more question, is there a way to keep the old log intact? – crasyboy42 Jan 15 '16 at 20:01
  • Sure, you can replace the `>` with `>>` (to mean append) - will update the answer – mfitzp Jan 15 '16 at 20:05