Magellan Linux

Diff of /mage/trunk/include/mtools.minc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4046 by niro, Mon Aug 29 20:14:35 2011 UTC revision 4047 by niro, Tue Nov 6 12:57:02 2012 UTC
# Line 1  Line 1 
1  # $Header: /alx-cvs/mage-eglibc/include/mtools.minc,v 1.1.1.1 2008/03/05 15:10:52 niro Exp $  # $Id$
2    
3  # adds services to runlevels and starts them  # adds services to runlevels and starts them
4  # mstartservice service {/path/to/service_exec}  # mstartservice service {/path/to/service_exec}
# Line 7  mstartservice() Line 7  mstartservice()
7   local service="$1"   local service="$1"
8   local service_exec="$2"   local service_exec="$2"
9    
10     # only run if sysV init was found
11     if [[ ! -f /etc/rc.d/init.d/functions ]]
12     then
13     echo "sysV init not found, not adding service ${service} to runlevels!"
14     return
15     fi
16    
17     # don't run if feature !autosvc was set
18     if mqueryfeature "!autosvc"
19     then
20     echo "!autosvc detected; auto management of services disabled."
21     return
22     fi
23    
24   [[ -z ${service_exec} ]] && service_exec="$(which ${service})"   [[ -z ${service_exec} ]] && service_exec="$(which ${service})"
25    
26   # add service to default runlevels   # add service to default runlevels
27   echo "Adding ${service} to default runlevels ..."   echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding ${service} to default runlevels ..."
28   [[ -x ${MROOT}/etc/rc.d/init.d/${service} ]] && rc-config add ${service} &> /dev/null   [[ -x ${MROOT}/etc/rc.d/init.d/${service} ]] && rc-config add ${service} &> /dev/null
29    
30   # do not start services on bootstrap or MROOT!=/   # do not start services on bootstrap or MROOT!=/
# Line 35  mstopservice() Line 49  mstopservice()
49   local service="$1"   local service="$1"
50   local service_exec="$2"   local service_exec="$2"
51    
52   [[ -z ${service_exec} ]] && service_exec="$(which ${service})"   # only run if sysV init was found
53     if [[ ! -f /etc/rc.d/init.d/functions ]]
54     then
55     echo "sysV init not found, not adding service ${service} to runlevels!"
56     return
57     fi
58    
59     # don't run if feature !autosvc was set
60     if mqueryfeature "!autosvc"
61     then
62     echo "!autosvc detected; auto management of services disabled."
63     return
64     fi
65    
66     if [[ -z ${service_exec} ]]
67     then
68     service_exec="$(which ${service})"
69     elif [[ x$(basename ${service_exec}) = x${service_exec} ]]
70     then
71     # expand full path
72     service_exec="$(which ${service_exec})"
73     fi
74    
75   # only stop the service if ${service_exec} does not exist   # only stop the service if ${service_exec} does not exist
76   [[ -f ${MROOT}/${service_exec} ]] && return   [[ -x ${MROOT}/${service_exec} ]] && return
77    
78   # del services from runlevel regardless if they exist or not   # del services from runlevel regardless if they exist or not
79   echo "Removing ${service} from default runlevels ..."   echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing ${service} from default runlevels ..."
80   rc-config del ${service} &> /dev/null   rc-config del ${service} &> /dev/null
81    
82   # do not stop services on bootstrap or MROOT!=/   # do not stop services on bootstrap or MROOT!=/
# Line 55  mstopservice() Line 90  mstopservice()
90   killall -9 ${service_exec} &> /dev/null   killall -9 ${service_exec} &> /dev/null
91   fi   fi
92   fi   fi
 }  
93    }
94    
95    # reloads a service if already running
96    # mreloadservice service {/path/to/service_exec}
97    mreloadservice()
98    {
99     local service="$1"
100     local service_exec="$2"
101    
102     # only run if sysV init was found
103     if [[ ! -f /etc/rc.d/init.d/functions ]]
104     then
105     echo "sysV init not found, not adding service ${service} to runlevels!"
106     return
107     fi
108    
109     # don't run if feature !autosvc was set
110     if mqueryfeature "!autosvc"
111     then
112     echo "!autosvc detected; auto management of services disabled."
113     return
114     fi
115    
116     [[ -z ${service_exec} ]] && service_exec="$(which ${service})"
117    
118     # do not stop services on bootstrap or MROOT!=/
119     if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
120     then
121     # only reload the service if running
122     if [[ -n $(pidof ${service_exec}) ]]
123     then
124     /etc/init.d/${service} reload
125     fi
126     fi
127    }
128    
129    # adds systemd units to runlevels and starts them
130    # mstartunit service {/path/to/service_exec}
131    mstartunit()
132    {
133     local service="$1"
134     local service_exec="$2"
135     local opts
136    
137     # only run if systemd was found
138     if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]]
139     then
140     echo "systemd not found, not adding unit ${service} to runlevels!"
141     return
142     fi
143    
144     # don't run if feature !autosvc was set
145     if mqueryfeature "!autosvc"
146     then
147     echo "!autosvc detected; auto management of services disabled."
148     return
149     fi
150    
151     if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]]
152     then
153     opts="--root ${MROOT}"
154     fi
155    
156     if [[ -z ${service_exec} ]]
157     then
158     case ${service} in
159     *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;;
160     *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;;
161     *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;;
162     *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;;
163     *) service_exec="$(which ${service} 2> /dev/null)" ;;
164     esac
165     fi
166    
167     # add service to default runlevels
168     echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} adding unit ${service} to default runlevels ..."
169     # reload daemon to honor changed unit files
170     systemctl ${opts} daemon-reload
171     systemctl ${opts} enable ${service}
172    
173     # do not start services on bootstrap or MROOT!=/
174     if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
175     then
176     echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} starting unit ${service} ..."
177     # start or restart the service
178     # dont use try-restart, works only with active services!
179     systemctl restart ${service}
180     fi
181    }
182    
183    # removes systemd units from runlevels and stops them
184    # mstopunit service {/path/to/service_exec}
185    mstopunit()
186    {
187     local service="$1"
188     local service_exec="$2"
189     local opts
190    
191     # only run if systemd was found
192     if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]]
193     then
194     echo "systemd not found, not removing unit ${service} from runlevels!"
195     return
196     fi
197    
198     # don't run if feature !autosvc was set
199     if mqueryfeature "!autosvc"
200     then
201     echo "!autosvc detected; auto management of services disabled."
202     return
203     fi
204    
205     if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]]
206     then
207     opts="--root ${MROOT}"
208     fi
209    
210     if [[ -z ${service_exec} ]]
211     then
212     case ${service} in
213     *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;;
214     *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;;
215     *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;;
216     *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;;
217     *) service_exec="$(which ${service} 2> /dev/null)" ;;
218     esac
219     elif [[ x$(basename ${service_exec}) = x${service_exec} ]]
220     then
221     # expand full path
222     service_exec="$(which ${service_exec})"
223     fi
224    
225     # only stop the service if ${service_exec} does not exist
226     [[ -x ${MROOT}/${service_exec} ]] && return
227    
228     # del services from runlevel regardless if they exist or not
229     echo -e " ${COLBLUE}[${COLRED}-${COLBLUE}]${COLDEFAULT} removing unit ${service} from default runlevels ..."
230     # reload daemon to honor changed unit files
231     systemctl ${opts} daemon-reload
232     systemctl ${opts} disable ${service}
233    
234     # do not stop services on bootstrap or MROOT!=/
235     if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
236     then
237     echo -e " ${COLBLUE}[${COLGREEN}+${COLBLUE}]${COLDEFAULT} stopping unit ${service} ..."
238     systemctl stop ${service}
239     # try harder
240     if [[ -n $(pidof ${service_exec}) ]]
241     then
242     killall -15 ${service_exec} &> /dev/null
243     sleep 1
244     killall -9 ${service_exec} &> /dev/null
245     fi
246     fi
247    }
248    
249    # reloads a systemd unit if already running
250    # mreloadunit service {/path/to/service_exec}
251    mreloadunit()
252    {
253     local service="$1"
254     local service_exec="$2"
255     local opts
256    
257     # only run if systemd was found
258     if [[ ! -x /bin/systemctl ]] && [[ ! -x /usr/bin/systemctl ]]
259     then
260     echo "systemd not found, not removing unit ${service} from runlevels!"
261     return
262     fi
263    
264     # don't run if feature !autosvc was set
265     if mqueryfeature "!autosvc"
266     then
267     echo "!autosvc detected; auto management of services disabled."
268     return
269     fi
270    
271     if [[ ${MROOT} != / ]] && [[ ! -z ${MROOT} ]]
272     then
273     opts="--root ${MROOT}"
274     fi
275    
276     if [[ -z ${service_exec} ]]
277     then
278     case ${service} in
279     *.service) service_exec="$(which ${service%%.service} 2> /dev/null)" ;;
280     *.socket) service_exec="$(which ${service%%.socket} 2> /dev/null)" ;;
281     *.mount) service_exec="$(which ${service%%.mount} 2> /dev/null)" ;;
282     *.target) service_exec="$(which ${service%%.target} 2> /dev/null)" ;;
283     *) service_exec="$(which ${service} 2> /dev/null)" ;;
284     esac
285     fi
286    
287     # reload daemon to honor changed unit files
288     systemctl ${opts} daemon-reload
289    
290     # do not stop services on bootstrap or MROOT!=/
291     if [[ ${MAGE_BOOTSTRAP} != true ]] && [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
292     then
293     # only reload the service if running
294     systemctl reload-or-try-restart ${service}
295     fi
296    }

Legend:
Removed from v.4046  
changed lines
  Added in v.4047