updated on Sun Jan 15 16:02:00 UTC 2012
[aur-mirror.git] / bumblebee-git / bumblebeed.in
blob1f53abe9b2053d911e48dfe66d7040b853d24822
1 #!/bin/bash
2 # Bumblebee daemon handler script. Distro-independent script to start/stop
3 # daemon. Should be runnable in any distro but won't give any feedback.
5 . /etc/rc.conf
6 . /etc/rc.d/functions
8 NAME=bumblebeed
9 CLIENT="$(which optirun)"
10 BIN="$(which $NAME)"
11 PIDFILE=/var/run/$NAME.pid
13 start() {
14 # Start the daemon only if there is not another instance running
15 stat_busy "Starting Bumblebee"
16 local pid="$(cat "$PIDFILE" 2>/dev/null)"
17 kill -0 $pid >/dev/null 2>&1
18 case $? in
19 0) ;; # already running
20 *) # Can be started
21 "$BIN" --daemon >/dev/null
22 add_daemon $NAME
23 stat_done
24 return 0
26 esac
27 stat_fail
28 return 1
31 stop() {
32 # Stop the daemon only if there is an instance running
33 stat_busy "Stopping Bumblebee"
34 local pid="$(cat "$PIDFILE" 2>/dev/null)"
35 kill -0 $pid >/dev/null 2>&1
36 case $? in
37 0) # Alive and running
38 local pid="$(cat "$PIDFILE" 2>/dev/null)"
39 kill -TERM $pid >/dev/null
40 # give it time to end gracefully...
41 local retries=10
42 while [ $retries -gt 0 ]; do
43 retries=$(expr $retries - 1)
44 kill -0 $pid >/dev/null 2>&1
45 case $? in
46 0) # not ready
47 sleep .5
49 *) # no need for polling anymore
50 break
52 esac
53 done
54 # ... otherwhise just terminate it.
55 kill -0 $pid >/dev/null 2>&1
56 case $? in
57 0) # still alive > Kill
58 kill -KILL $pid >/dev/null
62 esac
63 rm_daemon $NAME
64 stat_done
66 *) # Not started
67 stat_done
69 esac
72 restart() {
73 stop
74 sleep 0.5
75 start
78 case "$1" in
79 start)
80 start
82 stop)
83 stop
85 restart)
86 restart
89 echo "Usage: $0 {start|stop|restart|status}"
90 exit 1
92 esac