一般地,当在目标机器编译安装某个服务程序后,为了使服务能开机自启动和关机自停止,则需要将其添加为系统服务。但不同的Linux系统管理服务的方法不同,如Ubuntu使用update-rc.d命令,而RedHat则使用chkconfig命令。因此为了能自动识别系统的类型,减少人工控制,编写了一个简单的autosrv脚本,要求至少1个最多2个参数,特点如下:
● 第1个参数只能为install或uninstall,表示安装或卸载服务。
● 第2参数是可选的,表示系统名称,如果没有指定,那么会自动识别,若出现提示错误,则表示应该要显式指定系统名称了。
从上可知,自动识别的方法是获取lsb_release -a返回的文本再使用awk来匹配ubuntu,redhat,debian,centos这几个子串(忽略大小写)。要注意的是,返回的文本可能有所不同,当系统安装了LSB模块时,返回结果如下
没有安装时,返回结果如下
无论哪种情况,要提取分析的都是第2行文本,因此使用了sed -n '2p'。srv_name.body是用于生成最终服务脚本的部分代码文件,通常包含了start,stop,status,restart几个函数,只是没有包含前面的一部分,而这部分则由autosrv脚本来根据不同的系统生成不同的代码。![]()
● 第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![]()
15
if [ -z "$sysname" ]; then
16
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][Ed][Dd][Hh][Aa][Tt]/) print "redhat"; else if($0~/[Cc][Ee][Nn][Tt][Oo][Ss]/) print "centos"; else print ""}'`
17
if [ -z "$sysname" ]; then
18
echo "Unknown system, you can explicitly specify it with the second parameter to retry"
19
exit
20
fi
21
echo "Current system is $sysname"
22
fi
23![]()
24
create_file_ubuntu_debian()
25
{
26
cat << END > srv_name
27
#! /bin/bash
28
. /lib/lsb/init-functions
29![]()
30
END
31
cat srv_name.body >> srv_name
32
}
33![]()
34
create_file_redhat_centos()
35
{
36
cat << END > srv_name
37
#! /bin/bash
38
#chkconfig:2345 90 10
39
#description:srv_name
40![]()
41
. /etc/rc.d/init.d/functions
42![]()
43
END
44
cat srv_name.body >> srv_name
45
}
46![]()
47
copy_file()
48
{
49
cp srv_name /etc/init.d/srv_name
50
chmod u+x /etc/init.d/srv_name
51
}
52![]()
53
remove_file()
54
{
55
rm -f /etc/init.d/srv_name
56
}
57![]()
58
install_ubuntu_debian()
59
{
60
create_file_ubuntu_debian
61
copy_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
copy_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

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

103

104

105


