blob: c77ae3c9e872f469406710a7e51cf9f392793edf (
plain)
1 #!/bin/bash
2
3 NAME=lirc
4 USER=lirc
5 RUNDIR=/var/run/lirc
6 PIDFILE="$RUNDIR/lircd.pid"
7 STARTCMD="/usr/bin/sudo -b -H -u $USER /usr/sbin/lircd"
8 STOPCMD=""
9 STOPTIMEOUT=120
10
11 function getpid() {
12 if [ -z "$PIDFILE" ]; then
13 pid="$(pgrep -xfn "$STARTCMD")"
14 else
15 if [ -f "$PIDFILE" ]; then
16 pid=$(< $PIDFILE)
17 if [ ! -d /proc/"$pid" ]; then
18 echo "$NAME: removing stale pidfile $PIDFILE" >&2
19 rm -f "$PIDFILE"
20 unset pid
21 fi
22 fi
23 fi
24 echo "$pid"
25 }
26
27 case $1 in
28 start)
29 pid=$(getpid)
30 install -d -m 755 -o $USER $RUNDIR || exit 1
31 if [ -n "$pid" ]; then
32 echo "$NAME already running with pid $pid" >&2
33 exit 1
34 fi
35 eval "$STARTCMD"
36 ;;
37 stop)
38 pid=$(getpid)
39 if [ -n "$pid" ]; then
40 if [ -n "$STOPCMD" ]; then
41 eval "$STOPCMD"
42 else
43 kill "$pid"
44 fi
45 t=$(printf '%(%s)T' -1)
46 tend=$((t+STOPTIMEOUT))
47 while [ -d /proc/$pid -a $t -lt $tend ]; do
48 sleep 0.5
49 t=$(printf '%(%s)T' -1)
50 done
51 if [ -d /proc/"$pid" ]; then
52 echo "$NAME still running with pid $pid" >&2
53 else
54 [ -n "$PIDFILE" ] && rm -f "$PIDFILE"
55 fi
56 else
57 echo "$NAME is not running" >&2
58 fi
59 ;;
60 restart)
61 $0 stop
62 $0 start
63 ;;
64 status)
65 pid=$(getpid)
66 if [ -n "$pid" ]; then
67 echo "$NAME is running with pid $pid"
68 else
69 echo "$NAME is not running"
70 fi
71 ;;
72 *)
73 echo "usage: $0 [start|stop|restart|status]"
74 ;;
75 esac
|