• shell脚本实战案例--系统服务脚本


    目录

    1、源码编译安装nginx

    2、分别编写基于RHEL6和RHEL7的脚本。

    2.1 RHEL6的nginx系统服务脚本

    2.2 RHEL7的nginx系统服务脚本


    1、源码编译安装nginx

    1. 1.首先关闭防火墙和selinux
    2. [root@node13 ~]# systemctl stop firewalld
    3. [root@node13 ~]# setenforce 0
    4. 2.准备环境(C和C++的编译环境)
    5. [root@node13 ~]# yum install -y gcc gcc-c++ make
    6. 3.下载nginx安装包(或者使用wget直接上传)
    7. 链接:http://nginx.org/download/
    8. 4.解压安装包
    9. [root@node13 ~]# tar xf nginx-1.22.0.tar.gz -C /usr/local/src/
    10. [root@node13 ~]# cd /usr/local/src/nginx-1.22.0/
    11. 5.安装依赖项,配置nginx
    12. [root@node13 nginx-1.22.0]# yum install -y pcre-devel zlib-devel
    13. [root@node13 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx
    14. 6.进行编译安装
    15. [root@node13 nginx-1.22.0]# make
    16. [root@node13 nginx-1.22.0]# make install

    2、分别编写基于RHEL6和RHEL7的脚本。

    2.1 RHEL6的nginx系统服务脚本

    【1】编写系统服务脚本(/etc/init.d/nginx)

    [root@node13 ~]# vim /etc/init.d/nginx        # 主要修改以下两句

    nginx=${NGINX-/usr/local/nginx/sbin/nginx}
    conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}

    1. #!/bin/sh
    2. #
    3. # nginx Startup script for nginx
    4. #
    5. # chkconfig: - 85 15
    6. # processname: nginx
    7. # config: /etc/nginx/nginx.conf
    8. # config: /etc/sysconfig/nginx
    9. # pidfile: /var/run/nginx.pid
    10. # description: nginx is an HTTP and reverse proxy server
    11. #
    12. ### BEGIN INIT INFO
    13. # Provides: nginx
    14. # Required-Start: $local_fs $remote_fs $network
    15. # Required-Stop: $local_fs $remote_fs $network
    16. # Default-Start: 2 3 4 5
    17. # Default-Stop: 0 1 6
    18. # Short-Description: start and stop nginx
    19. ### END INIT INFO
    20. # Source function library.
    21. . /etc/rc.d/init.d/functions
    22. if [ -L $0 ]; then
    23. initscript=`/bin/readlink -f $0`
    24. else
    25. initscript=$0
    26. fi
    27. sysconfig=`/bin/basename $initscript`
    28. if [ -f /etc/sysconfig/$sysconfig ]; then
    29. . /etc/sysconfig/$sysconfig
    30. fi
    31. nginx=${NGINX-/usr/local/nginx/sbin/nginx}
    32. prog=`/bin/basename $nginx`
    33. conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
    34. lockfile=${LOCKFILE-/var/lock/subsys/nginx}
    35. pidfile=${PIDFILE-/var/run/nginx.pid}
    36. SLEEPMSEC=${SLEEPMSEC-200000}
    37. UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
    38. RETVAL=0
    39. start() {
    40. echo -n $"Starting $prog: "
    41. daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
    42. RETVAL=$?
    43. echo
    44. [ $RETVAL = 0 ] && touch ${lockfile}
    45. return $RETVAL
    46. }
    47. stop() {
    48. echo -n $"Stopping $prog: "
    49. killproc -p ${pidfile} ${prog}
    50. RETVAL=$?
    51. echo
    52. [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
    53. }
    54. reload() {
    55. echo -n $"Reloading $prog: "
    56. killproc -p ${pidfile} ${prog} -HUP
    57. RETVAL=$?
    58. echo
    59. }
    60. upgrade() {
    61. oldbinpidfile=${pidfile}.oldbin
    62. configtest -q || return
    63. echo -n $"Starting new master $prog: "
    64. killproc -p ${pidfile} ${prog} -USR2
    65. echo
    66. for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
    67. /bin/usleep $SLEEPMSEC
    68. if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
    69. echo -n $"Graceful shutdown of old $prog: "
    70. killproc -p ${oldbinpidfile} ${prog} -QUIT
    71. RETVAL=$?
    72. echo
    73. return
    74. fi
    75. done
    76. echo $"Upgrade failed!"
    77. RETVAL=1
    78. }
    79. configtest() {
    80. if [ "$#" -ne 0 ] ; then
    81. case "$1" in
    82. -q)
    83. FLAG=$1
    84. ;;
    85. *)
    86. ;;
    87. esac
    88. shift
    89. fi
    90. ${nginx} -t -c ${conffile} $FLAG
    91. RETVAL=$?
    92. return $RETVAL
    93. }
    94. rh_status() {
    95. status -p ${pidfile} -b ${nginx} ${nginx}
    96. }
    97. # See how we were called.
    98. case "$1" in
    99. start)
    100. rh_status >/dev/null 2>&1 && exit 0
    101. start
    102. ;;
    103. stop)
    104. stop
    105. ;;
    106. status)
    107. rh_status
    108. RETVAL=$?
    109. ;;
    110. restart)
    111. configtest -q || exit $RETVAL
    112. stop
    113. start
    114. ;;
    115. upgrade)
    116. rh_status >/dev/null 2>&1 || exit 0
    117. upgrade
    118. ;;
    119. condrestart|try-restart)
    120. if rh_status >/dev/null 2>&1; then
    121. stop
    122. start
    123. fi
    124. ;;
    125. force-reload|reload)
    126. reload
    127. ;;
    128. configtest)
    129. configtest
    130. ;;
    131. *)
    132. echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
    133. RETVAL=2
    134. esac
    135. exit $RETVAL

     【2】修改配置文件(/usr/local/nginx/conf/nginx.conf)

    # 找到配置以下pid:

    pid        /var/run/nginx.pid;

    【3】增加执行权限

    [root@node13 init.d]# chmod +x nginx

    【4】添加系统服务,设置开机自启动

    [root@node13 init.d]# chkconfig --add nginx 
    [root@node13 init.d]# chkconfig nginx on
    [root@node13 init.d]# chkconfig --list nginx        # 查看服务列表

    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.

          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.

    nginx              0:off    1:off    2:on    3:on    4:on    5:on    6:off

    【5】测试脚本    

    [root@node13 init.d]# service nginx start 
    Starting nginx (via systemctl):                            [  OK  ]
    [root@node13 init.d]# netstat -lnupt | grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7026/nginx: master  

     在浏览器上输入自己的IP地址,看是否可以访问到nginx首页

     [root@node13 init.d]# service nginx stop        # 停止服务,看能否停止
    Stopping nginx (via systemctl):                            [  OK  ]
    [root@node13 init.d]# netstat -lnupt | grep 80        # 查看端口

     再次访问浏览器就访问不到了

    2.2 RHEL7的nginx系统服务脚本

    【1】编写系统服务脚本(/usr/lib/systemd/system/nginx.service)

    1. [Unit]
    2. Description=The nginx HTTP and reverse proxy server
    3. After=network-online.target remote-fs.target nss-lookup.target
    4. Wants=network-online.target
    5. [Service]
    6. Type=forking
    7. PIDFile=/run/nginx.pid
    8. ExecStartPre=/usr/bin/rm -f /run/nginx.pid
    9. ExecStartPre=/usr/local/nginx/sbin/nginx -t
    10. ExecStart=/usr/local/nginx/sbin/nginx
    11. ExecReload=/usr/sbin/nginx -s reload
    12. KillSignal=SIGQUIT
    13. TimeoutStopSec=5
    14. KillMode=process
    15. PrivateTmp=true
    16. [Install]
    17. WantedBy=multi-user.target

    【2】修改配置文件(/usr/local/nginx/conf/nginx.conf)

    # 找到配置以下pid:

    pid        /var/run/nginx.pid;

    【3】增加执行权限

    [root@node13 system]# chmod +x nginx.service

    【4】测试脚本

    [root@node13 system]# systemctl daemon-reload 
    [root@node13 system]# systemctl start nginx
    [root@node13 system]# netstat -lnupt | grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17196/nginx: master

    [root@node13 system]# systemctl stop nginx        
    [root@node13 system]# netstat -lnupt | grep 80

  • 相关阅读:
    呼叫中心的实时语音分析
    Java之IO流详解(三)——字符流
    grpc使用consul做服务注册与发现
    如何在JavaScript中使用for循环
    AI推介-大语言模型LLMs论文速览(arXiv方向):2024.03.05-2024.03.10—(1)
    【学习日志】2022.11.03 LearnOpenGL----DAY1
    java计算机毕业设计基于安卓Android/微信小程序的汽车租赁小程序-app
    生产报工异常信息提示器如何精确提醒管理人员
    MySQL数据库之主从复制及读写分离
    Java基础:常用类(四)
  • 原文地址:https://blog.csdn.net/m0_71840604/article/details/133974066