-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.py
73 lines (59 loc) · 1.87 KB
/
example.py
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):
"""
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):
"""
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 = {
"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)) # noqa: T201
install(label, myplist)
while job.pid is not None:
print("Alive! PID = %s" % job.pid) # noqa: T201
job.refresh()
time.sleep(0.2)
else:
if job.pid is None:
print("'%s' is loaded but not currently running" % (job.label)) # noqa: T201
else:
print("'%s' is loaded and currently running: PID = %s" % (job.label, job.pid)) # noqa: T201
while job.pid is not None:
print("Alive! PID = %s" % job.pid) # noqa: T201
job.refresh()
time.sleep(0.2)
print("Uninstalling again...") # noqa: T201
uninstall(label)
return 0
if __name__ == "__main__":
sys.exit(main())