Tags


I wrote a python processing daemon on a machine, that was supposed to run indefinitely which used to terminate mysteriously by itself. It was later traced to dying on logout, because I generally leave a machine terminal logged into the remote machine.

Later on, I checked if it was the notorious SIGHUP, a hangup signal issued to all processes when the user logs off.

In order to ascertain this, I used the following line to send a sighup signal to the process


sudo kill -SIGHUP 2025

You can get a list of process details(also containing the process id’s)


ps aux | grep python mydaemon.py

Now there are multiple ways of how to handle this:-

1) Using NOHUP:


sudo nohup python mydaemon.py

The problem with this approach is that I was calling system commands from my code which themselves were placed in /usr/bin. After using nohup to run the daemon, this access was restricted and my code threw exceptions saying that the commands were not found.

2) Using upstart:

This is a Ubuntu based daemon manager. It has all these features out of the box, sadly I havent had the time to use it. Its available on Redhat and other distros of linux as well.

3) Python:

Implementing a signal handler. This is by far the most convinient.


import signal
signal.signal(signal.SIG_HUP, signal.SIG_IGN)

SIG_IGN is a default signal handler that will do nothing. You could alternatively implement your own handler for various OS Signals in the following way:-


def handler(signum, frame):
print 'Did anyone HICHUP ?'
signal.signal(signal.SIG_HUP, signal.SIG_IGN)

If anyone has any better more professional ways of doing this, do let me know.

Advertisements