Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run python program as a daemon?

I write the following program to run my program as a daemon but it is not getting run; when i run the program from python debugger it works.

I am using Mac os x.

/User/Library/LaunchDaemons/com.bobbob.osx.test.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bobbob.osx.test</string>
<key>Program</key>
<string>/Users/vivekbhintade/Desktop/test.py</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

/Users/vivekbhintade/Desktop/test.py:

import urllib2
from datetime import datetime
import smtplib
from smtplib import SMTPException
import threading

def checkerror():

    #my code which works fine individually, which sends mail after 5 seconds to recipients.

checkerror()

And also i run the program from terminal with following command.

launchctl load /Library/LaunchDaemons/com.bobbob.osx.test.plist 

This does not result in any errors.

like image 330
Vivek Bhintade Avatar asked Sep 10 '13 12:09

Vivek Bhintade


1 Answers

You have almost certainly realised this in the intervening 3 months since posting, but there seems to be some confusion here between Launch Daemons and Launch Agents which I though it would be worth clearing up - especially since Agents are often referred to as Daemons.

Paraphrasing the Apple Developer library:

  • Daemons run as root at startup, are unable to present UI elements, and are located in /Library/LaunchDaemons/.
  • Agents run in a user context at login, and are able to present UI elements to the user. These are located in /Users/username/Library/LaunchAgents/.

Your program will not run as it's located in /User/Library - it needs to go into one or another of the paths mentioned above, depending on how you plan to use it.

like image 181
FarmerGedden Avatar answered Oct 27 '22 19:10

FarmerGedden