Skip to main content

pythonic interface for macOS launchd

Project description

https://img.shields.io/pypi/v/launchd.svg https://github.com/infothrill/python-launchd/actions/workflows/tests.yml/badge.svg?branch=main

launchd is a pythonic interface to interact with macOS’s launchd. It provides access to basic querying and interaction with launchd. It is implemented using the Objective C ServiceManagement framework as well as the launchd command line utility. Therefore, this python package can only be used on macOS

The python objective C bridge contains some special types. This package strips off all non built-in type information and returns pure python data.

Examples

The relevant import statement is:

import launchd

Listing all launchd jobs:

for job in launchd.jobs():
    print(job.label, job.pid, job.laststatus, job.properties, job.plistfilename)

Find the pid and laststatus of a job:

>>> launchd.LaunchdJob("com.apple.Finder").pid
278

>>> launchd.LaunchdJob("com.apple.Finder").laststatus
0

>>> launchd.LaunchdJob("com.example.fubar").pid
Traceback (most recent call last):
  File "launchd/launchctl.py", line 78, in refresh
    raise ValueError("job '%s' does not exist" % self.label)
ValueError: job 'com.example.fubar' does not exist

Detect if a job exists:

>>> launchd.LaunchdJob("com.example.fubar").exists()
False

launchd job properties (these come directly from launchd and NOT the .plist files):

>>> launchd.LaunchdJob("com.apple.Finder").properties
{'OnDemand': 1, 'PID': 278, 'PerJobMachServices': {'com.apple.coredrag': 0,
'com.apple.axserver': 0, 'com.apple.CFPasteboardClient': 0,
'com.apple.tsm.portname': 0}, 'LimitLoadToSessionType': 'Aqua',
'Program': '/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder',
'TimeOut': 30, 'LastExitStatus': 0, 'Label': 'com.apple.Finder',
'MachServices': {'com.apple.finder.ServiceProvider': 10}}

>>> launchd.LaunchdJob("com.apple.Finder").properties["OnDemand"]
1

Find all plist filenames of currently running jobs:

for job in launchd.jobs():
   if job.pid is None or job.plistfilename is None:
      continue
   print(job.plistfilename)

Job properties of a given job (this uses the actual .plist file):

>>> launchd.plist.read("com.apple.kextd")
{'ProgramArguments': ['/usr/libexec/kextd'], 'KeepAlive': {'SuccessfulExit': False},
'POSIXSpawnType': 'Interactive', 'MachServices': {'com.apple.KernelExtensionServer':
{'HostSpecialPort': 15}}, 'Label': 'com.apple.kextd'}

Installation

$ pip install launchd

or, if you want to work using the source tarball:

$ python setup.py install

Requirements

  • OS X >= 10.6
  • Python 3.4+

Release history

0.3.0 (June 2021)

  • changed: create directory hierarchy for plist file if not present. issue #6
  • improved: added automated flake8 tests, check-manifest and safety checks
  • changed: moved basic CI to GitHub actions

0.2.0 (March 2021)

  • drop python 2.x, 3.2, 3.3 support
  • fix plistlib calls (issue #4)

0.1.2 (September 2020)

  • added tox.ini for easier testing accross interpreter versions
  • added travis test setup
  • fixed incompatibility with launchctl in test code
  • fixed a typo in the README

0.1.1 (November 2013)

  • Fixed a bug in launchd.plist.read() when no scope was specified

0.1 (November 2013)

  • Focus: initial public release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

launchd-0.3.0.tar.gz (9.3 kB view hashes)

Uploaded source

Built Distribution

launchd-0.3.0-py3-none-any.whl (10.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page