1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 该函数的作用是启动一个可执行的二进制程序:
# 使用方法:
# 1、daemon {--check program|--check=program} [--user username|--user=username] [--pidfile pidfile|--# pidfile=pidfile] [+/- nice]
# 2、daemon [--user username|--user=username] [--pidfile pidfile|--pidfile=pidfile] [+/- nice] {program}
# [program options]
# 说明:
# 调用该函数时:
# 方法1:可以使用--check 来指定可运行二进制程序;
# 方法2:还可以在以破折号开头的参数后直接跟二进制程序.
# 但是,如果二进制程序运行时一定要带一些参数的话,最好使用方法2.
# 注意:
#     如果调用函数 daemon 启动一个二进制可执行程序时。没有指定pidfile文件所在的位# 置。有可能会造成程序的再次启动。因为,函数默认是到/var/run/目录下找一个进程的进程
# 号的。如果找不到就再次启动二进制程序。
# A function to start a program.
daemon() {
        
# Test syntax.
        
local 
gotbase= force= nicelevel corelimit  
        
# 设置局部变量,用来保存调用该函数传递过来的参数
        
local 
pid base= user= 
nice
bg
= pid_file=
        
nicelevel=0
        
while 
"$1" 
!= 
"${1##[-+]}" 
]; 
do  
# 该循环条件作用是:找出哪个参数是可执行程序。循环到可执行程序参数就结束while循环。
# 因为其它参数前面都有破折号标志(--),符合while循环条件.如:---user 或 --pidfile
# 所以调用该函数时:如果指定了运行该二进制程序的用户是谁--user 或 运行程序的pid文件# 等这些选项参数,调用该函数时,一定要把这些参数放在二进制可运行程序参数前。
# 由于可运行程序这个参数没有破折号标识,当while循环到该参数时,就结束循环。这带来一个
# 好处:有些二进制可运行程序运行时要带选项才能运行。就可以在二进制可运行程序后面添加
# 二进制执行程序的选项。
#    如:想运行nrpe这服务: 要指定配置文件和是以超级守护进程方式还是独立守护进程方式。
# daemon --pidfie=/var/run/nrped.pid /usr/local/nagios/bin/nrpe -c /etc/nagios/nrpe.conf -d
# 选项一般要使用破折号标明。
            
    
case 
$1 
in   
#下面的case是检测二进制执行程序前的其它参数的。把符合条件的参数使用前面定义的变量记录
#下来
            
''
)    
echo 
$
"$0: Usage: daemon [+/-nicelevel] {program}"
                   
return 
1;;
            
--check)
                   
base=$2
                   
gotbase=
"yes"
                   
shift 
2
                   
;;
            
--check=?*)
                   
base=${1
#--check=}
                   
gotbase=
"yes"
                   
shift
                   
;;
            
--user)
                   
user=$2
                   
shift 
2
                   
;;
            
--user=?*)
                   
user=${1
#--user=}
                   
shift
                   
;;
            
--pidfile)
                   
pid_file=$2
                   
shift 
2
                   
;;
            
--pidfile=?*)
                   
pid_file=${1
#--pidfile=}
                   
shift
                   
;;
            
--force)
                   
force=
"force"
                   
shift
                   
;;
            
[-+][0-9]*)
                   
nice
=
"nice -n $1"
                   
shift
                   
;;
            
*)     
echo 
$
"$0: Usage: daemon [+/-nicelevel] {program}"
                   
return 
1;;
          
esac
        
done
        
#####################################################################
        
# Save basename.
        
[ -z 
"$gotbase" 
] && base=${1
##*/} 
# 如果没有使用--check来指定二进制执行程序就执行 && 后面的参数替换。找出二进制执行
# 程序并保存在变量里。                            
        
# See if it's already running. Look *only* at the pid file.
        
__pids_var_run 
"$base" 
"$pid_file"  
# 判断该二进制程序是否运行的。
        
[ -n 
"$pid" 
-a -z 
"$force" 
] && 
return 
        
# 如果找到进程的进程号, 直接返回。就不往下执行了。
        
# make sure it doesn't core dump anywhere unless requested           # 资源限定
        
corelimit=
"ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
        
# if they set NICELEVEL in /etc/sysconfig/foo, honor it
        
[ -n 
"${NICELEVEL:-}" 
] && 
nice
=
"nice -n $NICELEVEL"
        
# Echo daemon     
        
"${BOOTUP:-}" 
"verbose" 
-a -z 
"${LSB:-}" 
] && 
echo 
-n 
" $base"
        
# And start it up. # 如果bash shell 执行到这,就启动二进制程序。
        
if 
[ -z 
"$user" 
]; 
then   
           
$
nice 
/bin/bash 
-c 
"$corelimit >/dev/null 2>&1 ; $*"  
           
# 启动该二进制程序
        
else
           
$
nice 
runuser -s 
/bin/bash 
- $user -c 
"$corelimit >/dev/null 2>&1 ; $*"         
           
#如果调用该函数时指定了--user就使用该方式启动启                                                                               
        
fi
        
"$?" 
-
eq 
0 ] && success $
"$base startup" 
|| failure $
"$base startup" 
        
#根据 if 语句的执行结果$?来判断启动二进制程序是否成功
       
#启动成功就调用函数:success 反之则调用函数:failure
}