Python daemon will not run in the background on Ubuntu
SOLVED. I was under the impression that the foreground
and the start
parameter was two different things. It turns out I just needed to do the following.
def run(self): while True: time.sleep(2)
to
def start(self): while True: time.sleep(2)
I then removed the foreground
parameter, because I can run the script from the terminal using the start
command to see the output in the foreground.
python /opt/my-daemon.py start
Also, in rc.local
I start the script as follows:
python /opt/my-daemon.py start &
This hides the daemon process and executes the script on startup regardless of the user who logs in :)
Instead of using daemon.py, you may want to consider leveraging Ubuntu's Upstart system which provides an easy way to set up a respawning daemon. From the same link, it features:
* Services may be respawned if they die unexpectedly* Supervision and respawning of daemons which separate from their parent process
If you are using Ubuntu9.10 or later, take a look at /etc/init/cron.conf as an example. For earlier versions of Ubuntu, I believe the upstart scripts are located in /etc/event.d/.
For an explanation of Upstart keywords, see here.