Sunday, November 27, 2011

Running child shell even after parent shell closes

Once in my project I was facing a strange problem. We have created a service script to start our product as a service in *nix. To start the service one need to give the command "service start" command on command prompt. However, to ease user we also provided a GUI with a start button to start the service. Now here starts the interesting part of story.

When user starts the service using the UI and as soon as closes the UI after starting the service, the service also used to get stop. After banging my head against the PC hundred times I got the clue and solution :)..

Reason for the above behavior: the SIGHUP signal

SIGHUP is a signal sent to a process when its controlling terminal ( or parent terminal) is closed. This signal causes all child processes to terminate when the terminal which launched them is closed. In my case, the GUI was launched using a shell script, and when the UI was closed, the shell script gets closed and thus the launched service as well.

Solution : NOHUP

NOHUP is a command to ignore SIGHUP signal, enabling the command to keep running even after the controlling terminal terminates. The nohup syntax :

nohup command-name &


And pufffff.... magic was done

And on the last note, nohup prevents logging so to get logs you must redirect I/O stream to some text file using > indirection

Stay tuned....


No comments: