jeruki
jeruki

Reputation: 1900

Redirect to a file output of remote python app started through ssh

[EDITED]

I have a python app in a remote server that i need to debug, when I run the app locally it prints some debug information (including python tracebacks) that i need to monitor.

Thanks to jeremy i got to monitor the output file using tail -F and studying his code I got here where i found the following variation of his command:

ssh root@$IP 'nohup python /root/python/run_dev_server.py &>> /var/log/myapp.log &'

This gets me almost exactly what i want, loggin information and python tracebacks, but i do not get any of the information displayed using print from python which i need.

so I also tried his command:

ssh root@$IP 'nohup python /root/python/run_dev_server.py 2>&1 >> /var/log/myapp.log &'

it logs in the file the output of the program from print and also the logging information, but all the tracebacks are lost so i cannot debug the python exceptions.

Is there a way I can capture all the information produced by the app?

Thanks in advance for any suggestion.

Upvotes: 2

Views: 976

Answers (1)

jeremy
jeremy

Reputation: 4314

I would suggest doing something like this:

/usr/bin/nohup COMMAND  ARGS 2>&1 >> /var/log/COMMAND.log &
/bin/echo $! > /var/run/COMMAND.pid

nohup keeps the process alive after your terminal/ssh session is closed, >> will save all stdout and stderr to /var/log/COMMAND.log for you to "tail -f" later on.

To get the stacktrace output (which you could print to stdout, or do something fancy like email it) add the following lines to your python code.


    import sys
    import traceback
    _old_excepthook = sys.excepthook
    def myexcepthook(exctype, value, tb):
            # if exctype == KeyboardInterrupt:  # handle keyboard events
            # Print to stdout for now, but could email or anything here.
            print traceback.print_tb(tb);
            _old_excepthook(exctype, value, traceback)
    sys.excepthook = myexcepthook

This will catch all exceptions (Including keyboard interrupts, so be careful)

Upvotes: 2

Related Questions