随笔-150  评论-223  文章-30  trackbacks-0
脚本概述
   一般地,当在目标机器编译安装某个服务程序后,为了使服务能开机自启动和关机自停止,则需要将其添加为系统服务。但不同的Linux系统管理服务的方法不同,如Ubuntu使用update-rc.d命令,而RedHat则使用chkconfig命令。因此为了能自动识别系统的类型,减少人工控制,编写了一个简单的autosrv脚本,要求至少1个最多2个参数,特点如下:
   ● 第1个参数只能为install或uninstall,表示安装或卸载服务。
   ● 第2参数是可选的,表示系统名称,如果没有指定,那么会自动识别,若出现提示错误,则表示应该要显式指定系统名称了。

脚本实现
  1#! /bin/bash
  2# autosrv
  3
  4if [ $# -lt 1 ]; then
  5    echo "Usage: $(basename "$0") install | uninstall [sysname]"
  6    exit
  7elif [ "$1" != "install" -a "$1" != "uninstall" ]; then
  8    echo "The first parameter must be install or uninstall" 
  9    exit
 10fi
 11
 12action=$1
 13sysname=$2
 14srv_path=/etc/init.d/srv_name
 15
 16if [ -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"
 23fi
 24
 25create_file_ubuntu_debian()
 26{
 27cat << END > $srv_path
 28#! /bin/bash
 29. /lib/lsb/init-functions
 30
 31END
 32cat srv_name.body >> $srv_path
 33}
 34
 35create_file_redhat_centos()
 36{
 37cat << END > $srv_path
 38#! /bin/bash
 39#chkconfig:2345 90 10
 40#description: srv name
 41
 42. /etc/rc.d/init.d/functions
 43
 44END
 45cat srv_name.body >> $srv_path
 46}
 47
 48chmod_file()
 49{
 50    chmod u+x $srv_path
 51}
 52
 53remove_file()
 54{
 55    rm -f $srv_path
 56}
 57
 58install_ubuntu_debian()
 59{
 60    create_file_ubuntu_debian
 61    chmod_file
 62    update-rc.d srv_name defaults 90 10
 63}
 64
 65uninstall_ubuntu_debian()
 66{
 67    update-rc.d -f srv_name remove
 68    remove_file
 69}
 70
 71install_redhat_centos()
 72{
 73    create_file_redhat_centos
 74    chmod_file
 75    chkconfig --add srv_name
 76}
 77
 78uninstall_redhat_centos()
 79{
 80    chkconfig --del srv_name
 81    remove_file
 82}
 83
 84case "$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    ;;
105esac
   从上可知,自动识别的方法是获取lsb_release -a返回的文本再使用awk来匹配ubuntu,redhat,debian,centos这几个子串(忽略大小写)。要注意的是,返回的文本可能有所不同。

   当系统安装了LSB模块时,返回结果如下
   

   没有安装时,返回结果如下
   
   无论哪种情况,要提取分析的都是第2行文本,因此使用了sed -n '2p'。srv_name.body是不同系统相同的用于生成最终服务脚本的部分代码文件,通常包含了start,stop,status,restart几个函数,只是没有包含前面的一部分,而这部分则由autosrv脚本来根据不同的系统生成不同的代码。
posted on 2014-01-03 17:11 春秋十二月 阅读(1910) 评论(1)  编辑 收藏 引用 所属分类: System

评论:
# re: shell应用(5): 自动生成并安装服务脚本 2014-01-03 21:23 | 梁.兄
不错,看来你linux熟很多了  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理