最近在学习linux系统的启动,第一个小函数,实现像linux系统中出现的服务启动失败,启动成功
以下为照片为例,启动tserver脚本,显示成功与否
以下是仿照/etc/init.d/funtions写的函数
/mnt/sysroot/etc/init.d/funtions
SCREEN=`stty -F /dev/console size 2>/dev/null`
LENGTH=${SCREEN#* }
[ -z $LENGTH ] && LENGTH=80
red="\033[31m"
green="\033[1;32m"
yellow="\033[1;33m"
blue="\033[34m"
reverse="\033[0m"
success () {
string=$1
SPA_LENGTH=$[$LENGTH - ${#string} -
22]
echo -n "$string"
for I in `seq 1 $SPA_LENGTH`;do
echo -n "
"
done
echo -e "[
${green}OK${reverse}
]"
}
failure() {
string=$1
SPA_LENGTH=$[$LENGTH - ${#string} -
22]
echo -n "$string"
for I in `seq 1 $SPA_LENGTH`; do
echo -n " "
done
echo -e "[
${yellow}FAILED$reverse ]
"
}
以下为服务脚本
#!/bin/bash
#
# chckconfig 35 33 66
# description test server
. /root/space_calu.sh
prog=`basename $0`
lockfile=/var/lock/subsys/$prog
start() {
touch $lockfile
}
stop () {
[ -e $lockfile ] && rm -rf
$lockfile
}
Usage () {
echo "Usage: $prog
{start|stop|staus|restart}"
}
case $1 in
start)
start
[ $? -eq 0 ] && success "$prog starting"
|| failure "$prog starting";;
stop)
stop
[ $? -eq 0 ] && success "$prog stoping"
|| failure "$prog stoping";;
staus)
[ -e $lockfile ] && echo "$prog is
starting." || echo "$prog is stop.";;
restart)
stop
[ $? -eq 0 ] && success "$prog stoping"
|| failure "$prog stoping"
start
[ $? -eq 0 ] && success "$prog starting"
|| failure "$prog starting";;
stop)
start;;
*)
Usage;;
esac