更新时间:2022-09-13 13:34:13
在做Nagios实验中,需要反复通过命令重启Nagios服务和Ndo2db服务,非常麻烦。所以写了个Ndo2db启动脚本,供参考!
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
|
#!/bin/bash # Copyright 1999-2013 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 # 调用functions,操作系统是Gentoo,functions在/etc/init.d目录 if [ -f /etc/rc .d /init .d /functions ]; then
. /etc/rc .d /init .d /functions
elif [ -f /etc/init .d /functions ]; then
. /etc/init .d /functions
fi # 定义变量,一般情况下只需要修改prefix&&Ndo2dbBin就可以使用 prefix= "/var/www/localhost/htdocs/nagios"
Ndo2dbBin=${prefix} /bin/ndo2db-3x
Ndo2dbCfgFile=${prefix} /etc/ndo2db .cfg
Ndo2dbVarDir=${prefix} /var
Ndo2dbRunFile=${prefix} /var/ndo2db .lock
Ndo2dbCGIDir=${prefix} /sbin
Ndo2dbUser=nagios Ndo2dbGroup=nagios # 判断ndo2db是否启动,如果启动读取进程号赋予Ndo2dbPID pid_ndo2db () { if [ ! -f $Ndo2dbRunFile ]; then
echo "Ndo2db is already stoped."
exit 1
else Ndo2dbPID=` head -n 1 $Ndo2dbRunFile`
fi } # 没什么好说的,杀死Ndo2db进程 killproc_ndo2db () { kill $Ndo2dbPID
} # 根据var/ndo2db.lock来判断ndo2db服务状态 printstatus_ndo2db () { if [ ! -f $Ndo2dbRunFile ]; then
echo "ndo2db is not running"
else echo "ndo2db (pid $Ndo2dbPID) is running..."
fi } # 确认存在ndo2dbbin文件,否则非法退出。 if [ ! -f $Ndo2dbBin ]; then
echo "executable file $Ndo2dbBin not found. Exiting."
exit 1
fi # 确认存在ndo2db配置文件,否则非法退出。 if [ ! -f $Ndo2dbCfgFile ]; then
echo "Configuration file $Ndo2dbCfgFile not found. Exiting."
exit 1
fi # start开启服务,stop停止服务,status查看服务状态,restart重启服务 case "$1" in
start) echo -n "starting ndo2db:"
$Ndo2dbBin -c $Ndo2dbCfgFile echo " done."
;; stop) echo -n "stoping ndo2db:"
pid_ndo2db killproc_ndo2db killall -q ndo2db-3x echo " done."
;; status) pid_ndo2db printstatus_ndo2db ;; restart) $0 stop $0 start ;; *) echo "Usage: ndo2db {start|stop|restart|status}"
exit 1
;; esac # 实际操作 # 1、拷贝脚本到/etc/init.d下,vi ndo2db # 2、添加脚本执行权限chmod +x ndo2db # 3、启动服务/etc/init.d/ndo2db start,停止服务/etc/init.d/ndo2db stop,查看服务/etc/init.d/ndo2db status,重启服务/etc/init.d/ndo2db restart. # 初次写模块化脚本,如果有问题,请指正,谢谢! |