/[vps]/baselayout-vserver/trunk/bin/rc-status
Gentoo

Contents of /baselayout-vserver/trunk/bin/rc-status

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sat Sep 3 16:10:27 2005 UTC (9 years, 10 months ago) by hollow
File size: 8495 byte(s)
import initial baselayout sources (1.12.0_pre8)
1 #!/bin/bash
2 ################################################################################
3 # #
4 # Author: Sean E. Russell <ser@germane-software.com> #
5 # Version: 1.0 #
6 # Date: Jun 26, 2002 #
7 # Adaptation: Mike Frysinger [SpanKY] <vapier@gentoo.org> #
8 # Original code was in Ruby ... recoded into bash (at syntax level) #
9 # #
10 # This application displays information about the RC system used by Gentoo. #
11 # In particular, it displays a tree-like format of a run level, showing #
12 # all of the services that are installed at that level, and what each #
13 # service's status is (running, stopped, etc.) #
14 # #
15 # -a can be used to display all runlevels #
16 # -d can be used to display service dependancies #
17 # -u will display all unassigned services #
18 # -s will display all services #
19 # -h will display help #
20 # <runlevel> is used to choose the run level for which information is #
21 # displayed #
22 # #
23 # By default, rc-status only displays information about the current #
24 # runlevel; services installed and services running. #
25 # #
26 ################################################################################
27
28 # grab code from functions.sh so we don't have to reproduce it
29 source /sbin/functions.sh
30 runleveldir=/etc/runlevels
31
32 # grab settings from conf.d/rc
33 source /etc/conf.d/rc
34 source "${svclib}/sh/rc-daemon.sh"
35
36 ################################################################################
37 # Parse command line options #
38 ################################################################################
39 do_opt() {
40 case $1 in
41 --all|-a)
42 ALL=true
43 ;;
44 --depend)
45 DEPEND=true
46 ;;
47 --unused|-u)
48 ALL=true
49 UNUSED=true
50 ;;
51 --list|-l)
52 ls -1 ${runleveldir}
53 exit 0
54 ;;
55 --servicelist|-s)
56 ALL=true
57 SERVICELIST=true
58 ;;
59 --nocolor|-nc)
60 ;;
61 --help|-h|-*)
62 echo "USAGE: $0 [command | <runlevel>]"
63 echo
64 echo "Commands:"
65 echo " -a, --all Show services at all run levels"
66 echo " -l, --list Show list of run levels"
67 echo " -u, --unused Show services not assigned to any run level"
68 echo " -s, --servicelist Show service list"
69 echo " -nc,--nocolor Monochrome output only"
70 echo " <runlevel> Show services assigned to <runlevel>"
71 echo
72 echo "If no arguments are supplied, shows services for current run level."
73 exit 0
74 ;;
75 *)
76 runlevel=$1
77 ;;
78 esac
79 }
80 for opt in "$@" ; do
81 do_opt ${opt}
82 [[ -n $2 ]] && shift
83 done
84
85 ################################################################################
86 # Find the current runlevel being queried. This is either something supplied #
87 # on the command line, or pulled from softlevel #
88 ################################################################################
89 if [[ -z ${runlevel} ]] ; then
90 if [[ -e ${svcdir}/softlevel ]] ; then
91 runlevel=$(<${svcdir}/softlevel)
92 else
93 ewarn "Could not local current runlevel in ${svcdir}/softlevel"
94 if [[ -d ${runleveldir}/single ]] ; then
95 runlevel=single
96 elif [[ -d ${runleveldir}/default ]] ; then
97 runlevel=default
98 else
99 eerror "Your installation is probably broken ... please \`emerge baselayout\`"
100 exit 1
101 fi
102 ewarn "Assuming current runrevel is '${runlevel}'"
103 fi
104 fi
105 if [[ ! -d ${runleveldir}/${runlevel} ]] ; then
106 eerror "${runlevel} is not a valid run level !"
107 eerror "Valid runlevels (obtained from \`rc-status --list\`):"
108 rc-status --list
109 exit 1
110 fi
111
112 ################################################################################
113 # Build up a hash of the services associated with each run level. In the most #
114 # trivial case, this is simply the current runlevel. If --all was specified, #
115 # we gather information about all of the runlevels. If --unused was #
116 # specified, we pull info about all of the services and filter for the ones #
117 # that don't appear in any runlevel. #
118 ################################################################################
119 runlevelidxs=$(ls ${runleveldir})
120 declare -a runlevels
121 # For each directory in /etc/runlevels, do ...
122 arridx=0
123 for level in ${runlevelidxs} ; do
124 if [[ ${level} == ${runlevel} || -n ${ALL} ]] ; then
125 runlevels[${arridx}]=$(find ${runleveldir}/${level} -maxdepth 1 -type l -printf '%f ')
126 let "arridx += 1"
127 fi
128 done
129
130 # In case --all was specified, get a list of all the services set up in
131 # /etc/init.d; services can be added, but not enabled, and we need to
132 # identify these 'orphan' services.
133 in_list() { #$1=list $2=find
134 for ele in $1 ; do
135 if [[ ${ele} == $2 ]] ; then
136 echo 1
137 return 0
138 fi
139 done
140 echo 0
141 return 0
142 }
143 if [[ -n ${ALL} ]] ; then
144 unassigned=
145 allservices=
146 for service in $(ls -1 /etc/init.d | grep -v '\.sh$') ; do
147 if [[ $(in_list "${runlevels[*]}" "${service}") -eq 0 ]] ; then
148 unassigned="${unassigned} ${service}"
149 fi
150 allservices="${allservices} ${service}"
151 done
152 runlevelidxs="${runlevelidxs} UNASSIGNED"
153 runlevels[${arridx}]="${unassigned}"
154 runlevels[${arridx}+1]="${allservices}"
155 fi
156
157 ################################################################################
158 # Now collect information about the status of the various services; whether #
159 # they're started, broken, or failed. Put all of this into arrays. #
160 ################################################################################
161 if [[ -x ${svcdir}/started ]]; then
162 started=$(ls ${svcdir}/started)
163 # If we're root then update service statuses incase any naughty daemons
164 # stopped running without our say so
165 if [[ ${EUID} == 0 ]]; then
166 for service in ${started}; do
167 update_service_status "${service}"
168 done
169 started=$(ls ${svcdir}/started)
170 fi
171 fi
172 [[ -x ${svcdir}/starting ]] && starting=$(ls ${svcdir}/starting)
173 [[ -x ${svcdir}/inactive ]] && inactive=$(ls ${svcdir}/inactive)
174 [[ -x ${svcdir}/stopping ]] && stopping=$(ls ${svcdir}/stopping)
175
176 ################################################################################
177 # Now print out the information we've gathered. We do this by going through #
178 # the hash of 'runlevels' information, and for each String key/Array value #
179 # pair, print the runlevel; then for each service in that runlevel, print the #
180 # service name and its status. #
181 ################################################################################
182 # Define a helper method for printing the status of a service; '[ xxx ]'
183 print_msg() {
184 printf " %-$((COLS - 5 - ${#3}))s%s\n" "$1" "${BRACKET}[ $2$3 ${BRACKET}]${NORMAL}"
185 }
186
187 # if --all wasnt specified, dont print everything
188 [[ -z ${ALL} ]] && runlevelidxs=${runlevel}
189 if [[ -z ${UNUSED} ]] ; then
190 if [[ -z ${SERVICELIST} ]] ; then
191 arridx=0
192 else
193 runlevelidxs="all"
194 let "arridx += 1"
195 fi
196 else
197 runlevelidxs="unused"
198 fi
199
200 for level in ${runlevelidxs} ; do
201 echo "Runlevel: ${HILITE}${level}${NORMAL}"
202 for service in ${runlevels[${arridx}]} ; do
203 if [[ -n ${inactive} && $(in_list "${inactive}" "${service}") -eq 1 ]] ; then
204 print_msg "${service}" "${WARN}" 'inactive'
205 elif [[ $(in_list "${started}" "${service}") -eq 1 ]] ; then
206 print_msg "${service}" "${GOOD}" 'started'
207 elif [[ -n ${starting} && $(in_list "${starting}" "${service}") -eq 1 ]] ; then
208 print_msg "${service}" "${GOOD}" 'starting'
209 elif [[ -n ${stopping} && $(in_list "${stopping}" "${service}") -eq 1 ]] ; then
210 print_msg "${service}" "${BAD}" 'stopping'
211 else
212 print_msg "${service}" "${BAD}" 'stopped'
213 fi
214 done
215 let "arridx += 1"
216 [ -n "${UNUSED}" ] && exit 0
217 done

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20