用shell编写nginx脚本的启动,关闭,重加载

前端之家收集整理的这篇文章主要介绍了用shell编写nginx脚本的启动,关闭,重加载前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #!/bin/bash----默认执行shell方式
  2. #chkconfig:23451080----加入到开机执行的方式
  3. path="/usr/local/Nginx/sbin/Nginx"----源代码安装Nginx之后的启动路径
  4. name="Nginxd"
  5. test=0
  6. log=/tmp/Nginxd.log----Nginx的日志
  7. DATE=`date"+%F%H:%M:%S"`----获得系统时间命令
  8.  
  9.  
  10.  
  11. #判断Nginx是否已经安装或者判断源码安装之后启动的路径是否存在。
  12. if[!-x$path];then
  13. echo-n"${path}notinstalled!"
  14. exit10
  15. fi
  16.  
  17. #启动选项,对执行Nginx进行启动,启动成功或者失败都会输出提醒。
  18. start(){
  19. echo-n"starting$name;"
  20. $path-t
  21. test=$?
  22. if[$test-eq0];then
  23. touch/tmp/Nginx.pid
  24. $path
  25. echo"$DATEand$pathisstarting">>$log
  26. else
  27. echo"pleasecheckyouconfig"
  28. exit20
  29. fi
  30. }
  31.  
  32.  
  33. #停止Nginx服务,并把输出的信息重定向到指定的日志文件
  34. stop(){
  35. echo-n"stopping$name;"
  36. ps-ef|grepNginx|awk'{print$2}'|xargskill-9
  37. test=0
  38. if[$test-eq0];then
  39. rm-rf/tmp/Nginx.pid
  40. echo"$DATEand$pathisstop">>$log
  41. fi
  42. }
  43.  
  44.  
  45. #重加载选项,将服务重新加载并识别服务是否正常运行。
  46. reload(){
  47. echo-n"reloading$name;"
  48. # ps-ef|grepNginx|awk'{print$2}'|xargskill-9
  49. $path-t
  50. test=$?
  51. if[$test-eq0];then
  52. touch/tmp/reload.pid
  53. echo"$DATEand$pathisreloading">>$log
  54. rm-rf/tmp/reload.pid
  55. else
  56. echo"pleasecheckyouconfig"
  57. exit20
  58. fi
  59. }
  60.  
  61.  
  62. #整个脚本使用case方式来进行调用,当执行该脚本时会输出相关信息提醒。
  63. case"$1"in
  64. start)start;;
  65. stop)stop;;
  66. reload)reload;;
  67. *)echo"/usr/local/Nginx/sbin/Nginxstart|stop|reload!";;
  68. esac
  69.  
  70.  
  71.  
  72.  
  73. ######---退出shell之后,执行以下操作---######
  74.  
  75. 1.文件加权限
  76. chmod+xNginxd
  77.  
  78. 2.文件执行
  79. bashNginxd[start|stop|reload]

猜你在找的Bash相关文章