add proper error handling for all final exec calls
[hband-tools.git] / user-tools / daemonctl
blob4583cbeb4f714ebce566053243da8ebb216f5e24
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 daemonctl - Manage preconfigured libslack daemon(1) daemons more conveniently
10 =head1 DESCRIPTIONS
12 Daemonctl presumes some facts about the system:
14 =over 8
16 =item daemons are configured in F</etc/daemon.conf>
18 =item daemons log to /syslog/daemon/daemon.<DAEMON>/today.log
20 =back
22 =cut
24 EOF
27 set +e
28 set -u
30 . /usr/lib/tool/bash-utils
32 _status()
34 local daemon status pid started code
35 daemon=$1
37 status=`daemon -n "$daemon" -v --running 2>&1`
38 code=$?
39 if [ $code = 0 ]
40 then
41 pid=`head -n1 /var/run/daemons/$daemon.pid`
42 started=`ps o lstart='' -p $pid`
43 status="$status (running since $started)"
45 echo "$status"
46 return $code
49 status()
51 _status "$@"
52 if [ $? != 0 ]
53 then
55 echo -n "known daemons: "
56 list_all_daemons | tr "\n" " "
57 echo
58 }>&2
62 list_pidfile_daemons()
64 ls -1 /var/run/daemons | sed -e 's/\.pid$//'
67 _filter_daemon_names()
69 perl -ne '($s) = /^\s*(\S+)/; $s =~ /^[^*#]/ and print "$s\n"'
72 list_configured_daemons()
74 cat /etc/daemon.conf | perl -pe 's/^#DISABLED# //' | _filter_daemon_names
77 list_enabled_daemons()
79 cat /etc/daemon.conf | _filter_daemon_names
82 list_all_daemons()
85 list_pidfile_daemons
86 list_configured_daemons
87 }|\
88 sort -u
91 if [ $# = 0 ]
92 then
93 list_all_daemons |\
94 while read daemon
96 _status "$daemon"
97 done
98 exit
101 daemon=$1
102 shift
103 verb=''
104 if [ $# -gt 0 ]
105 then
106 verb=$1
107 shift
110 status "$daemon"
112 case "$verb" in
113 start)
114 if list_enabled_daemons | grep -qx "$daemon"
115 then
116 daemon -n "$daemon" -v
117 else
118 errx 1 "$daemon: not sure, is it enabled in /etc/daemon.conf ?"
121 stop)
122 daemon -n "$daemon" -v --stop
124 disable)
125 cat /etc/daemon.conf | daemon="$daemon" perl -pe 's/^\Q$ENV{daemon}\E\s/#DISABLED# $&/' | sponge /etc/daemon.conf
127 enable)
128 cat /etc/daemon.conf | daemon="$daemon" perl -pe 's/^#DISABLED# (\Q$ENV{daemon}\E)/$1/' | sponge /etc/daemon.conf
130 restart)
131 daemon -n "$daemon" -v --restart
133 log)
134 tail "$@" -f /syslog/daemon/daemon.$daemon/`date +%Y%m%d`.log
135 exit
137 ''|status)
138 # status is already shown above
139 exit
142 warnx "unknown verb '$verb'"
143 warnx "valid verbs: start, restart, stop, log, disable, enable, status"
144 false
146 esac
148 _status "$daemon"