CarpeNoctem
CarpeNoctem

Reputation: 5660

Troubleshoot python daemon that quits unexpectedly?

What's the best way to monitor a python daemon to determine the cause of it quitting unexpectedly? Is strace my best option or is there something Python specific that does the job?

Upvotes: 4

Views: 2256

Answers (3)

blob42
blob42

Reputation: 549

As the answer above, try to add Logging, however be carefull if you are using python-daemon module it will not work with Logging module when logging to a file, so you should make the logging manually to a file.

Also, make your daemon restarts after it has failed by running it inside a loop and catch exceptions inside the loop.

Example:

while True:
    try:
        log_to_file("starting daemon")
        run_daemon()
        log_to_file("daemon stopped unexpectedly")
        sleep(1)
    except Exception as err:
        log_to_file(err)

Upvotes: 0

Zach Kelling
Zach Kelling

Reputation: 53819

You can use pdb:

python -m pdb myscript.py

Running your program like this will cause it to enter post-mortem debugging if it exits abnormally. If you have an idea where the problem is you can also use import pdb; pdb.set_trace() at the point you want to start debugging. Also logging profusely helps.

Upvotes: 0

Daenyth
Daenyth

Reputation: 37441

I would generally start by adding logging to it. At a minimum, have whatever is launching it capture stdout/stderr so that any stack traces are saved. Examine your except blocks to make sure you're not capturing exceptions silently.

Upvotes: 1

Related Questions