nginx自动启动脚本

红太狼 2021-10-29 11:44 553阅读 0赞

nginx自动启动脚本

环境准备提示:

启动服务命令为:/application/nginx/sbin/nginx
停止服务命令为: /application/nginx/sbin/nginx -s stop
请用case语句开发脚本,以实现Nginx服务启动及关闭的功能,具体脚本命令为/etc/init.d/nginxd {status|start|stop|restart}

  1. 如果读者对Ngnix环境还不是很熟悉,那么请参考跟老男孩学\\<<Linux运维:Web集群实战>>第5章的内容。

参考资料:
https://blog.51cto.com/398528/2053566

  1. # nginx安装
  2. cd /opt/
  3. wget https://ftp.pcre.org/pub/pcre/pcre-8.36.tar.gz
  4. tar -zxvf pcre-8.36.tar.gz
  5. cd pcre-8.36
  6. ./configure
  7. make && make install
  8. cd /opt/
  9. wget http://nginx.org/download/nginx-1.12.2.tar.gz
  10. tar -zxvf nginx-1.12.2.tar.gz
  11. useradd nginx -s /sbin/nologin -M
  12. yum -y install pcre-devel
  13. cd nginx-1.12.2
  14. ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.36
  15. make && make install
  16. ln -s /application/nginx-1.12.2 /application/nginx
  17. /application/nginx/sbin/nginx -t
  18. 启动
  19. /application/nginx/sbin/nginx
  20. 查看端口
  21. netstat -lnt
  22. ps -ef|grep nginx
  23. 反查
  24. lsof -i :8080
  25. nginx主页文件所在位置
  26. /application/nginx/conf/nginx.conf

-——————————————————————————————————————————————————————————
解题思路:
1 ) 先判断Ngnix的PID文件是否存在(Nginx服务正常启动后PID文件就会存在),如果不存在,即表示Nginx没有运行,则运行Nginx服务的启动命令
(可以把此部分写成start函数)。待要停止时,如果PID存在,就运行Nginx服务停止命令,否则就不运行停止命令(可以把此部分写成stop函数)。
2 ) 通过脚本传参start或stop等,通过case语句获取参数进行判断。
3 ) 为了看起来更专业,这里采用前文讲解的系统函数functions中的action函数。
4 ) 对函数及命令运行的返回值进行处理,使脚本看起来更专业、规范。
5 ) 通过chkconfig来管理Nginx脚本,实现开机自动启。

  1. cat >/etc/init.d/nginxd<<"EOF"
  2. #!/bin/bash
  3. # chkconfig: 2345 40 98
  4. # dectiption: Start/Stop Nginx server
  5. path="/application/nginx/sbin"
  6. pid="/application/nginx/logs/nginx.pid"
  7. RETVAL=0 # 设定RETVAL为0,作为返回值变量。
  8. . /etc/init.d/functions
  9. # start
  10. function usage(){
  11. echo $"`basename $0` {status|start|stop|restart}"
  12. exit 1
  13. }
  14. function status(){
  15. if [[ -f $pid ]]; then
  16. echo "nginx is running..."
  17. else
  18. echo "nginx is not running"
  19. fi
  20. exit $RETVAL
  21. }
  22. function start(){
  23. if [ ! -f $pid ]; then
  24. $path/nginx
  25. RETVAL=$?
  26. if [[ $RETVAL -eq 0 ]]; then
  27. action "nginx is started" /bin/true
  28. else
  29. action "nginx is started" /bin/false
  30. return RETVAL
  31. fi
  32. else
  33. echo "nginx is running..."
  34. return 0
  35. fi
  36. }
  37. function stop(){
  38. if [[ -f $pid ]]; then
  39. $path/nginx -s stop
  40. RETVAL=$?
  41. if [[ $RETVAL -eq 0 ]]; then
  42. action "nginx is stopped" /bin/true
  43. return $RETVAL
  44. else
  45. action "nginx is stopped" /bin/false
  46. return $RETVAL
  47. fi
  48. else
  49. echo "nginx is no running"
  50. return $RETVAL
  51. fi
  52. }
  53. case "$1" in
  54. start )
  55. start
  56. RETVAL=$?
  57. ;;
  58. stop )
  59. stop
  60. RETVAL=$?
  61. ;;
  62. restart )
  63. stop
  64. sleep 3
  65. start
  66. RETVAL=$?
  67. ;;
  68. status )
  69. status
  70. RETVAL=$?
  71. ;;
  72. * )
  73. usage
  74. esac
  75. exit $RETVAL
  76. EOF
  77. chmod +x /etc/init.d/nginxd
  78. [root@db nginx-1.12.2]# /etc/init.d/nginxd status
  79. nginx is running...
  80. [root@db nginx-1.12.2]# /etc/init.d/nginxd stop
  81. nginx is stopped [ OK ]
  82. [root@db nginx-1.12.2]# /etc/init.d/nginxd status
  83. nginx is not running
  84. [root@db nginx-1.12.2]# /etc/init.d/nginxd start
  85. nginx is started [ OK ]
  86. [root@db nginx-1.12.2]# /etc/init.d/nginxd stop
  87. nginx is stopped [ OK ]
  88. [root@db nginx-1.12.2]# /etc/init.d/nginxd restart
  89. nginx is no running
  90. nginx is started [ OK ]
  91. [root@db nginx-1.12.2]# /etc/init.d/nginxd restart
  92. nginx is stopped [ OK ]
  93. nginx is started [ OK ]
  94. [root@db nginx-1.12.2]#

转载于:https://www.cnblogs.com/bjx2020/p/11176636.html

发表评论

表情:
评论列表 (有 0 条评论,553人围观)

还没有评论,来说两句吧...

相关阅读