forked from infothrill/python-launchd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
73 lines (59 loc) Β· 1.86 KB
/
example.py
File metadata and controls
73 lines (59 loc) Β· 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
Example script showing how to install and remove plist based launchd jobs
"""
import sys
import os
import launchd
def install(label, plist):
'''
Utility function to store a new .plist file and load it
:param label: job label
:param plist: a property list dictionary
'''
fname = launchd.plist.write(label, plist)
launchd.load(fname)
def uninstall(label):
'''
Utility function to remove a .plist file and unload it
:param label: job label
'''
if launchd.LaunchdJob(label).exists():
fname = launchd.plist.discover_filename(label)
launchd.unload(fname)
os.unlink(fname)
def main():
myplist = dict(
Disabled=False,
Label="testlaunchdwrapper_python",
Nice=-15,
OnDemand=True,
ProgramArguments=["/bin/bash", "-c", "sleep 1 && echo 'Hello World' && exit 0"],
RunAtLoad=True,
ServiceDescription="runs a sample command",
ServiceIPC=False,
)
import time
label = myplist['Label']
job = launchd.LaunchdJob(label)
if not job.exists():
print("'%s' is not loaded in launchd. Installing..." % (label))
install(label, myplist)
while job.pid is not None:
print("Alive! PID = %s" % job.pid)
job.refresh()
time.sleep(0.2)
else:
if job.pid is None:
print("'%s' is loaded but not currently running" % (job.label))
else:
print("'%s' is loaded and currently running: PID = %s" % (job.label, job.pid))
while job.pid is not None:
print("Alive! PID = %s" % job.pid)
job.refresh()
time.sleep(0.2)
print("Uninstalling again...")
uninstall(label)
return 0
if __name__ == '__main__':
sys.exit(main())