# 检测一个二进制可运行程序是否运行:# 使用方法:
status [-p pidfile] {program}status() {
local base pid pid_file=
# Test syntax. # 测试调用该函数时的参数格式。
if [ "$#" = 0 ] ; then
echo $"Usage: status [-p pidfile] {program}"
# 如果调用该函数没有给一个参数的话就显示:该函数的使用方法,然后直
#接退出。退出状态码:1
return 1 fi
if [ "$1" = "-p" ]; then
pid_file=$2
shift 2 fi
base=${1##*/} # 使用$base 记录二进制可运行程序的文件名。
# First try "pidof"
__pids_var_run "$1" "$pid_file"
# 调用函数:__pids_var_run 来检测二进制程序的运行状态。
RC=$?
if [ -z "$pid_file" -a -z "$pid" ]; then
# 如果调用 status时,没有传递pidfile文件,并且没有检测到PID号
pid="$(__pids_pidof "$1")"
# 因为有些二进制可运行程序,是不会把PID写到
# /var/run/$base.pid文件当中的。就调用函数__pids_pidof来查找PID号
fi####################################################################
if [ -n "$pid" ]; then
# 如果查到二进制运行程序的PID号,就代表该该二进制程序是运行的。
echo $"${base} (pid $pid) is running..."
return 0 fi
case "$RC" in
# 根据函数:__pids_var_run返回的状态码,来判断二进制程序的运行状态。
0)
echo $"${base} (pid $pid) is running..."
return 0 ;;
1)
echo $"${base} dead but pid file exists"
# 如果函数:__pids_var_run返回的状态码是:1 ,的话....
return 1 ;;
esac#####################################################################
# See if /var/lock/subsys/${base} exists # ***锁文件。
if [ -f /var/lock/subsys/${base} ]; then
echo $"${base} dead but subsys locked"
return 2 fi
echo $"${base} is stopped"
return 3}
https://blog.51cto.com/9528du/1420154