Question

I'm trying to install an OSX LaunchDaemon using python but calling launchctl using subprocess.Popen doesn't actually install the service.

I have the plist file in /Library/LaunchDaemons/ and I can load the plist file just fine using the terminal:

$ launchctl load -w /Library/LaunchDaemons/com.myplist.file.plist

$ launchctl start com.myplist.file

$ launchctl list

"- 0 com.myplist.file"

The service loads and starts properly through the command line, meaning my plist file is correctly set up, But the problem starts when I execute the same commands with python subprocess.Popen or any python system call command.

            # Load the service
            command = shlex.split("launchctl load -w /Library/LaunchDaemons/com.myplist.file.plist")
            subprocess.Popen(command)
            # Start the service
            command = shlex.split("launchctl start com.myplist.file")
            subprocess.Popen(command)

I've also tried setting shell=True but no luck. Any thoughts or ideas on this?

Était-ce utile?

La solution

I've figured it out! Thanks for the help, self. Oh, you're welcome, self!

Anyone wanting to install OSX services via python will find this useful.

Load the service

servicePath = '/Library/LaunchDaemons/com.myplist.file.plist'

launchctlCmd = ['/bin/launchctl', 'load', '-w', servicePath]
# Execute service load command
proc = subprocess.Popen(launchctlCmd, shell=False, bufsize=1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Start the service

serviceName = 'com.myplist.file'

launchctlCmd = ['/bin/launchctl', 'start', serviceName]
# Execute service start command
proc = subprocess.Popen(launchctlCmd, shell=False, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top