Quantcast
Channel: C++博客-所有随笔
Viewing all articles
Browse latest Browse all 7882

shell应用(5): 自动生成并安装服务脚本

$
0
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
 14
 15if [ -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"
 22fi
 23
 24create_file_ubuntu_debian()
 25{
 26ca<< END > srv_name
 27#! /bin/bash
 28. /lib/lsb/init-functions
 29
 30END
 31cat srv_name.body >> srv_name
 32}
 33
 34create_file_redhat_centos()
 35{
 36cat << END > srv_name
 37#! /bin/bash
 38#chkconfig:2345 90 10
 39#description:srv_name
 40
 41. /etc/rc.d/init.d/functions
 42
 43END
 44cat srv_name.body >> srv_name
 45}
 46
 47copy_file()
 48{
 49    cp srv_name /etc/init.d/srv_name
 50    chmod u+x /etc/init.d/srv_name
 51}
 52
 53remove_file()
 54{
 55    rm -f /etc/init.d/srv_name
 56}
 57
 58install_ubuntu_debian()
 59{
 60    create_file_ubuntu_debian
 61    copy_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    copy_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脚本来根据不同的系统生成不同的代码。


春秋十二月 2014-01-03 17:11 发表评论

Viewing all articles
Browse latest Browse all 7882

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>