/
bumblebeed.in
93 lines (78 loc) · 1.9 KB
/
bumblebeed.in
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
91
92
93
#!/bin/sh
### BEGIN INIT INFO
# Provides: bumblebeed
# Required-Start: $localfs $syslog nvidia-kernel
# Required-Stop: $localfs $syslog nvidia-kernel
# Should-Start: kdm gdm
# Should-Stop: kdm gdm
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Bumblebee supporting nVidia Optimus cards
# Description: Daemon responsible for handling power management for
# nVidia Optimus cards and handling optirun requests which
# allows the discrete card to be used
### END INIT INFO
# Bumblebee daemon handler script. Distro-independent script to start/stop
# daemon. Should be runnable in any distro but won't give any feedback.
# Avoid bashism since this script runs using /bin/sh, not /bin/bash!
NAME=bumblebeed
BIN='@SBINDIR@/bumblebeed'
PIDFILE=/var/run/$NAME.pid
# returns 0 if running, non-zero otherwise
status() {
local pid
# program is not running
[ -s "$PIDFILE" ] || return 3
pid="$(cat "$PIDFILE" 2>/dev/null)"
# process is not running, pid file not properly cleared
kill -0 "$pid" || return 1
# process is running
return 0
}
start() {
# program is not installed
[ -x "$BIN" ] || return 5
# return if already started
status && return 0 || true
"$BIN" --daemon
status && return 0 || return 1
}
stop() {
local retries=10
local pid="$(cat "$PIDFILE" 2>/dev/null)"
# not running, we're done
status || return 0
# first ask the daemon nicely to quit
kill "$pid" || true
# and check whether it listened
while [ $retries -gt 0 ]; do
retries=$((retries - 1))
# process has gone, return success
status || return 0
# wait for half a minut before polling again
sleep .5
done
# failed to stop or timeout
return 1
}
restart() {
stop && start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac