7

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.

3
  • 1
    does launchctl load /Library/LaunchDaemons/com.bobbob.osx.test.plist work? Commented Sep 10, 2013 at 12:46
  • yes it work means it does not giving any kind of error msg Commented Sep 10, 2013 at 12:47
  • 2
    But does launchctl also send the email you expect? How do you run it from the terminal? With $ python test.py? It seems to me that #!/bin/env python is missing at the beginning of your file, so the os doesn't know how to run it. Commented Sep 10, 2013 at 13:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried launchctl?

I believe this stack answers your question:

Running Python in background on OS X

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.