Shell应用(5): 自动生成并安装服务脚本
脚本概述
一般地,当在目标机器编译安装某个服务程序后,为了使服务能开机自启动和关机自停止,则需要将其添加为系统服务。但不同的Linux系统管理服务的方法不同,如Ubuntu使用update-rc.d命令,而RedHat则使用chkconfig命令。因此为了能自动识别系统的类型,减少人工控制,编写了一个简单的autosrv脚本,要求至少1个最多2个参数,特点如下:
● 第1个参数只能为install或uninstall,表示安装或卸载服务。
● 第2参数是可选的,表示系统名称,如果没有指定,那么会自动识别,若出现提示错误,则表示应该要显式指定系统名称了。
脚本实现
从上可知,自动识别的方法是获取lsb_release -a返回的文本再使用awk来匹配ubuntu,redhat,debian,centos这几个子串(忽略大小写)。要注意的是,返回的文本可能有所不同。
当系统安装了LSB模块时,返回结果如下
![]()
没有安装时,返回结果如下
![]()
一般地,当在目标机器编译安装某个服务程序后,为了使服务能开机自启动和关机自停止,则需要将其添加为系统服务。但不同的Linux系统管理服务的方法不同,如Ubuntu使用update-rc.d命令,而RedHat则使用chkconfig命令。因此为了能自动识别系统的类型,减少人工控制,编写了一个简单的autosrv脚本,要求至少1个最多2个参数,特点如下:
● 第1个参数只能为install或uninstall,表示安装或卸载服务。
● 第2参数是可选的,表示系统名称,如果没有指定,那么会自动识别,若出现提示错误,则表示应该要显式指定系统名称了。
脚本实现
1
#! /bin/bash
2
# autosrv
3![]()
4
if [ $# -lt 1 ]; then
5
echo "Usage: $(basename "$0") install | uninstall [sysname]"
6
exit
7
elif [ "$1" != "install" -a "$1" != "uninstall" ]; then
8
echo "The first parameter must be install or uninstall"
9
exit
10
fi
11![]()
12
action=$1
13
sysname=$2
14
srv_path=/etc/init.d/srv_name
15![]()
16
if [ -z "$sysname" ]; then
17
sysname=`lsb_release -a | sed -n '2p' | awk '{if($0~/[Uu][Bb][Uu][Nn][Tt][Uu]/) print "ubuntu"; else if($0~/[Dd][Ee][Bb][Ii][Aa][Nn]/) print "debian"; else if($0~/[Rr][Ee][Dd][Hh][Aa][Tt]/) print "redhat"; else if($0~/[Cc][Ee][Nn][Tt][Oo][Ss]/) print "centos"; else print ""}'`
18
if [ -z "$sysname" ]; then
19
echo "Unknown system, please manual special it with the second parameter"
20
exit
21
fi
22
echo "Current system is $sysname"
23
fi
24![]()
25
create_file_ubuntu_debian()
26
{
27
cat << END > $srv_path
28
#! /bin/bash
29
. /lib/lsb/init-functions
30![]()
31
END
32
cat srv_name.body >> $srv_path
33
}
34![]()
35
create_file_redhat_centos()
36
{
37
cat << END > $srv_path
38
#! /bin/bash
39
#chkconfig:2345 90 10
40
#description: srv name
41![]()
42
. /etc/rc.d/init.d/functions
43![]()
44
END
45
cat srv_name.body >> $srv_path
46
}
47![]()
48
chmod_file()
49
{
50
chmod u+x $srv_path
51
}
52![]()
53
remove_file()
54
{
55
rm -f $srv_path
56
}
57![]()
58
install_ubuntu_debian()
59
{
60
create_file_ubuntu_debian
61
chmod_file
62
update-rc.d srv_name defaults 90 10
63
}
64![]()
65
uninstall_ubuntu_debian()
66
{
67
update-rc.d -f srv_name remove
68
remove_file
69
}
70![]()
71
install_redhat_centos()
72
{
73
create_file_redhat_centos
74
chmod_file
75
chkconfig --add srv_name
76
}
77![]()
78
uninstall_redhat_centos()
79
{
80
chkconfig --del srv_name
81
remove_file
82
}
83![]()
84
case "$sysname" in
85
ubuntu|debian)
86
if [ "$action" = "install" ]; then
87
install_ubuntu_debian
88
else
89
uninstall_ubuntu_debian
90
fi
91
;;
92![]()
93
redhat|centos)
94
if [ "$action" = "install" ]; then
95
install_redhat_centos
96
else
97
uninstall_redhat_centos
98
fi
99
;;
100![]()
101
*)
102
echo "Currently only support ubuntu, debian, redhat and centos system"
103
exit
104
;;
105
esac
#! /bin/bash2
# autosrv3

4
if [ $# -lt 1 ]; then5
echo "Usage: $(basename "$0") install | uninstall [sysname]"6
exit7
elif [ "$1" != "install" -a "$1" != "uninstall" ]; then8
echo "The first parameter must be install or uninstall" 9
exit10
fi11

12
action=$113
sysname=$214
srv_path=/etc/init.d/srv_name15

16
if [ -z "$sysname" ]; then17
sysname=`lsb_release -a | sed -n '2p' | awk '{if($0~/[Uu][Bb][Uu][Nn][Tt][Uu]/) print "ubuntu"; else if($0~/[Dd][Ee][Bb][Ii][Aa][Nn]/) print "debian"; else if($0~/[Rr][Ee][Dd][Hh][Aa][Tt]/) print "redhat"; else if($0~/[Cc][Ee][Nn][Tt][Oo][Ss]/) print "centos"; else print ""}'`18
if [ -z "$sysname" ]; then19
echo "Unknown system, please manual special it with the second parameter"20
exit21
fi22
echo "Current system is $sysname"23
fi24

25
create_file_ubuntu_debian()26
{27
cat << END > $srv_path28
#! /bin/bash29
. /lib/lsb/init-functions30

31
END32
cat srv_name.body >> $srv_path33
}34

35
create_file_redhat_centos()36
{37
cat << END > $srv_path38
#! /bin/bash39
#chkconfig:2345 90 1040
#description: srv name41

42
. /etc/rc.d/init.d/functions43

44
END45
cat srv_name.body >> $srv_path46
}47

48
chmod_file()49
{50
chmod u+x $srv_path51
}52

53
remove_file()54
{55
rm -f $srv_path56
}57

58
install_ubuntu_debian()59
{60
create_file_ubuntu_debian61
chmod_file62
update-rc.d srv_name defaults 90 1063
}64

65
uninstall_ubuntu_debian()66
{67
update-rc.d -f srv_name remove68
remove_file69
}70

71
install_redhat_centos()72
{73
create_file_redhat_centos74
chmod_file75
chkconfig --add srv_name76
}77

78
uninstall_redhat_centos()79
{80
chkconfig --del srv_name81
remove_file82
}83

84
case "$sysname" in85
ubuntu|debian)86
if [ "$action" = "install" ]; then87
install_ubuntu_debian88
else89
uninstall_ubuntu_debian90
fi91
;;92

93
redhat|centos)94
if [ "$action" = "install" ]; then95
install_redhat_centos96
else97
uninstall_redhat_centos98
fi99
;;100

101
*)102
echo "Currently only support ubuntu, debian, redhat and centos system"103
exit104
;;105
esac当系统安装了LSB模块时,返回结果如下

没有安装时,返回结果如下

无论哪种情况,要提取分析的都是第2行文本,因此使用了sed -n '2p'。srv_name.body是不同系统相同的用于生成最终服务脚本的部分代码文件,通常包含了start,stop,status,restart几个函数,只是没有包含前面的一部分,而这部分则由autosrv脚本来根据不同的系统生成不同的代码。

浙公网安备 33010602011771号