-
/var/lock/subsys/* contain lockfiles for subsystems.
pid=`/bin/ps -e | /bin/awk '$4=="foo" { print $1 }'`
case $1 in
start)
[ $pid = '' ] && [ ! -e /var/lock/subsys/foo ] \
&& [ -x /usr/sbin/foo ] \
&& /bin/touch /var/lock/subsys/foo \
&& /usr/sbin/foo &
;;
stop)
[ $pid != '' ] && /bin/rm -f /var/lock/subsys/foo \
&& /bin/kill $pid
;;
*)
echo "usage: foo {start or stop}"
- add functions to kill process or start daemon:
#! /bin/sh
# Source function library.
. /etc/rc.d/init.d/functions
# Get config.
. /etc/sysconfig/network
# Check that networking is up.
if [ ${NETWORKING} = "no" ]
then
exit 0
fi
[ -f /var/local/ssh/sbin/sshd2 ] || exit 0
RETVAL=0
# See how we were called.
case "$1" in
start)
echo -n "Starting sshd: "
daemon /var/local/ssh/sbin/sshd2
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/sshd
;;
stop)
echo -n "Stopping sshd services: "
killproc sshd2
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/sshd
;;
status)
status sshd2
RETVAL=$?
;;
restart|reload)
$0 stop
$0 start
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload}"
exit 1
esac
exit $RETVAL
- here's where it gets complex: functions call others:
- A function to stop a program.
killproc() {
RC=0
# Test syntax.
if [ $# = 0 ]; then
echo "Usage: killproc {program} [signal]"
return 1
fi
notset=0
# check for second arg to be kill level
if [ "$2" != "" ] ; then
killlevel=$2
else
notset=1
killlevel="-9"
fi
# Save basename.
base=`basename $1`
# Find pid.
pidlist=`pidofproc $base`
pid=
for apid in $pidlist ; do
[ -d /proc/$apid ] && pid="$pid $apid"
done
# Kill it.
if [ "$pid" != "" ] ; then
[ $BOOTUP = "verbose" ] && echo -n "$base "
if [ "$notset" = "1" ] ; then
if ps h $pid>/dev/null 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid
usleep 100000
if ps h $pid >/dev/null 2>&1 ; then
sleep 1
if ps h $pid >/dev/null 2>&1 ; then
sleep 3
if ps h $pid >/dev/null 2>&1 ; then
kill -KILL $pid
fi
fi
fi
fi
ps h $pid >/dev/null 2>&1
RC=$?
[ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
RC=$((! $RC))
# use specified level only
else
if ps h $pid >/dev/null 2>&1; then
kill $killlevel $pid
RC=$?
[ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
fi
fi
else
failure "$base shutdown"
fi
# Remove pid file if any.
if [ "$notset" = "1" ]; then
rm -f /var/run/$base.pid
fi
return $RC
}
- what's really going on here?
-
killproc does a lot more than kill.
- mainly to assure that the process is `sincerely' dead.
- but it's not over yet:
- A function to find the pid of a program.
pidofproc() {
# Test syntax.
if [ $# = 0 ] ; then
echo "Usage: pidofproc {program}"
return 1
fi
# First try "/var/run/*.pid" files
if [ -f /var/run/$1.pid ] ; then
pid=`head -1 /var/run/$1.pid`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
fi
# Next try "pidof"
pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
if [ "$pid" != "" ] ; then
echo $pid
return 0
fi
}
- what's going on here?
- avoid calling 'ps'!
- call another function to do that if necessary.
- Gasp! Another function pidof: see
man pidof for details.