-
Notifications
You must be signed in to change notification settings - Fork 1
/
launchctl-unload
executable file
·90 lines (82 loc) · 1.68 KB
/
launchctl-unload
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
#
# Synopsis:
# Simple frontend for OSX launchctl to unload services.
# Usage:
# sudo -i
# launchctl-unload jmsdesk.blobio.*
#
# alias haltjms=launchctl-unload jmsdesk.blobio.*
# Note:
# is "stop" done by "unload"?
#
# How to send a sig9 to uncooperative processes?
#
LD=/Library/LaunchDaemons
UNLOAD_PAUSE_DURATION=3
log()
{
echo "$@"
}
die()
{
log "ERROR: $@" >&2
exit 1
}
WARN()
{
log "WARN (ok): $@" >&2
}
test $(pwd) = $LD && die "can not run in services dir: $D"
test $# = 1 || die "wrong argument count: got $#, expected 1"
cd $LD || die "cd $LD failed: exit status=$?"
for PLIST in $@; do
test -r $PLIST || die "can not read plist file: $PLIST"
if [[ "$PLIST" =~ ^.*\.plist$ ]]; then
SRV=$(basename $PLIST .plist)
else
die "service file must match *.plist: $PLIST"
fi
log "stopping $SRV"
launchctl stop $SRV
STATUS=$?
case $STATUS in
0)
log "service stopped: $SRV"
;;
3)
WARN 'process already stopped'
;;
*)
EMSG=$(launchctl error $STATUS)
die "launchctl stop failed: exit status: $EMSG"
;;
esac
log "unloading plist: $PLIST"
E=$(launchctl unload $PLIST 2>&1 | tail -1)
STATUS=$?
case $STATUS in
0)
#
# launchctl returns 0 when service already unloaded.
# also writes
#
# Unload failed: 113: Could not find specified service
#
# to standard error, so grep for that pattern.
#
# the pain never ends.
#
if [[ "$(echo $E | tail -1)" =~ ^.*:[[:space:]]113:.*$ ]]; then
WARN 'process not loaded'
elif [ -n "$EMSG" ]; then
die "launchctl: unexpected stderr: $EMSG"
fi
log "plist unloaded: $PLIST"
;;
*)
EMSG=$(launchctl error $STATUS)
die "launchctl stop failed: exit status: $EMSG"
;;
esac
done