#!/bin/sh # # run init scripts action=$1 direction=$(test "$1" = "start" -o "$1" = "resume" && echo "start" || echo "stop") name=$2 path=/var/spool/apm.init call() ( names="" for name in $(cat $1); do if [ -e /etc/init.d/$name -a ! -e $path/pid/$direction/$name ]; then names="$names $name" touch $path/pid/$direction/$name fi done for name in $names; do { test -e $path/pre/$action/$name && call $path/pre/$action/$name echo -e "$( (/etc/init.d/$name $action) 2>&1 )" rm -f $path/pid/$(test "$direction" = "start" && echo "stop" || echo "start")/$name } & echo $! > $path/pid/$direction/$name done for name in $(cat $1); do test -s $path/pid/$direction/$name && while [ -e /proc/$(cat $path/pid/$direction/$name 2>/dev/null) ]; do sleep 1 "#waiting for /etc/init.d/$name"; done; done ) init() ( rm -rf $path/pre $path/order test "$action" = "start" -a -z "$name" && rm -rf $path/pid for a in start stop resume halt; do mkdir -p $path/order/$a $path/pre/$a $path/pid/$a; done find /etc/init.d -maxdepth 1 -type f ! -name "rc.*" -printf "%f\n" | while read name; do ( . /etc/init.d/$name init if [ -n "$start" ]; then test -n "$before" && echo $name | tee $(echo "$before" | sed "s#\(\S\+\)#-a $path/pre/start/\1#g") >/dev/null test -n "$after" && echo $after >> $path/pre/start/$name echo $name >> $path/order/start/${priority:-0} fi if [ -n "$resume" ]; then test -n "$before" && echo $name | tee $(echo "$before" | sed "s#\(\S\+\)#-a $path/pre/resume/\1#g") >/dev/null test -n "$after" && echo $after >> $path/pre/resume/$name echo $name >> $path/order/resume/${priority:-0} fi if [ -n "$stop" ]; then test -n "$before" && echo $before >> $path/pre/stop/$name test -n "$after" && echo $name | tee $(echo "$after" | sed "s#\(\S\+\)#-a $path/pre/stop/\1#g") >/dev/null echo $name >> $path/order/stop/${priority:-0} fi if [ -n "$halt" ]; then test -n "$before" && echo $before >> $path/pre/halt/$name test -n "$after" && echo $name | tee $(echo "$after" | sed "s#\(\S\+\)#-a $path/pre/halt/\1#g") >/dev/null echo $name >> $path/order/halt/${priority:-0} fi ) done ) init if [ -z "$name" ]; then export init=1 ls -1 $path/order/$action | sort $(test "$direction" = "start" && echo "-r") | while read priority; do call $path/order/$action/$priority done else rm -f $path/pid/$direction/$name echo "$name" > $path/call call $path/call fi