草庐IT

Nginx启动脚本

丁丁历险 2023-03-28 原文
Nginx启动脚本


Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。因稳定性、丰富的功能、低资源消耗而闻名。
但Nginx本身不自带启动脚本,需要我们手动编写一份,现在网上所提供的大多数脚本都是有针对行的,可移植性很差。
大多数这样的脚本依赖于系统中functions函数,但该函数仅在个别系统中存在。
为了使脚本更加通用,以下编写的脚本可以很轻松的移植到各种Unix、Linux系统中,同时还兼容CentOS的chkconfig功能。
测试证明无需修改即可在CentOS、Ubuntu、FreeBSD上测试运行正常。

脚本的思路是通过nginx.pid文件来判断进程是否启动,当然如果你看了http://manual.blog.51cto.com/3300438/932958这篇文章,就可以通过awk过滤端口号判断进程是否开启,效果更好。
备注:通过pid判断服务的启动与否,可能会导致stop指令执行结束后pid文件没有及时删除(有延迟),而这时进行start启动服务会报服务已启动(而不会真的启动服务)。这种情况会在开启服务后进行restart指令时出现。所以通过端口判断服务的开启与否会更稳定,效果更好...
脚本提供了Nginx所支持的6种进程管理信号中的4种启动用控制信号,同时额外附件了一个查看进程状态的功能。
脚本全文如下:[root@centos6 ~] cat /etc/init.d/nginx

  1. #!/bin/sh 
  2. # Startup script for the Nginx 
  3. # chkconfig: - 88 63 
  4. # description: Nginx is a free,open-source,high-performance HTTP Server and reverse proxy. 
  5. # program:/usr/local/nginx/sbin/nginx 
  6. # config:/usr/local/nginx/conf/nginx.conf 
  7. # pidfile:/usr/local/nginx/logs/nginx.pid 
  8.  
  9. # Synopsis: 
  10. #        nginx [--help] [--version] {start|stop|restart|reload|status|update} 
  11.  
  12.  
  13. # Define variable 
  14. nginx=/usr/local/nginx/sbin/nginx 
  15. pidfile=/usr/local/nginx/logs/nginx.pid 
  16. PROGRAM=`basename $0` 
  17. VERSION=1.0 
  18. # Functions 
  19. usage(){ 
  20.     echo "Usage: $PROGRAM [--help] [--version] {start|stop|restart|reload|status|update}" 
  21.  
  22. version(){ 
  23.     echo "Version:$VERSION" 
  24.  
  25. start(){ 
  26. if [ -e $pidfile ] 
  27.    then 
  28.     echo "Nginx already running..." 
  29.    else 
  30.     echo -e "Starting Nginx:\t\t\t\t\t\t\t\c" 
  31.     /usr/local/nginx/sbin/nginx 
  32.     echo -e "[ \c" 
  33.     echo -e "\033[0;32mOK\033[0m\c" 
  34.     echo -e " ]\c" 
  35.     echo -e "\r" 
  36. fi 
  37.  
  38. stop(){ 
  39. if [ -e $pidfile ] 
  40.    then 
  41.     echo -e "Stopping Nginx:\t\t\t\t\t\t\t\c" 
  42.     kill -TERM `cat ${pidfile}` 
  43.     echo -e "[ \c" 
  44.     echo -e "\033[0;32mOK\033[0m\c" 
  45.     echo -e " ]\c" 
  46.     echo -e "\r" 
  47.    else 
  48.     echo "Nginx already stopped..." 
  49. fi 
  50.  
  51. reload(){ 
  52. if [ -e $pidfile ] 
  53.    then 
  54.     echo -e "Reloading Nginx:\t\t\t\t\t\t\c" 
  55.     kill -HUP `cat ${pidfile}` 
  56.     echo -e "[ \c" 
  57.     echo -e "\033[0;32mOK\033[0m\c" 
  58.     echo -e " ]\c" 
  59.     echo -e "\r" 
  60.    else 
  61.     echo "Nginx is not running..." 
  62. fi 
  63.  
  64. status(){ 
  65.     if [ -e $pidfile ] 
  66.        then 
  67.         PID=`cat $pidfile` 
  68.         echo  "Nginx (pid $PID) is running..." 
  69.        else 
  70.         echo  "Nginx is stopped" 
  71.     fi 
  72.  
  73. update(){ 
  74. if [ -e $pidfile ] 
  75.    then 
  76.     echo -e "Updateing Nginx:\t\t\t\t\t\t\c" 
  77.     kill -USR2 `cat ${pidfile}` 
  78.     echo -e "[ \c" 
  79.     echo -e "\033[0;32mOK\033[0m\c" 
  80.     echo -e " ]\c" 
  81.     echo -e "\r" 
  82.    else 
  83.     echo "Nginx is not running..." 
  84. fi 
  85. if [ $# -gt 0 ] 
  86.    then 
  87.     case $1 in 
  88.         start) 
  89.             start 
  90.             ;; 
  91.         stop) 
  92.             stop 
  93.             ;; 
  94.         restart) 
  95.             stop 
  96.             start 
  97.             ;; 
  98.         reload) 
  99.             reload 
  100.             ;; 
  101.         status) 
  102.             status 
  103.             ;; 
  104.         update) 
  105.             update 
  106.             ;; 
  107.         --help) 
  108.             usage 
  109.             ;; 
  110.         --version) 
  111.             version 
  112.             ;; 
  113.         *) 
  114.             usage 
  115.     esac 
  116.    else 
  117.     usage 
  118. fi 
如果你的系统使用的是CentOS或RedHat的系统,可以通过chkconfig将其设置为开机启动项。
[root@centos6 ~] chkconfig  --add  nginx
[root@centos6 ~] chkconfig  nginx  on
脚本运行效果如图:

 

有关Nginx启动脚本的更多相关文章

  1. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  2. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  3. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  4. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  5. UE4 源码阅读:从引擎启动到Receive Begin Play - 2

    一、引擎主循环UE版本:4.27一、引擎主循环的位置:Launch.cpp:GuardedMain函数二、、GuardedMain函数执行逻辑:1、EnginePreInit:加载大多数模块int32ErrorLevel=EnginePreInit(CmdLine);PreInit模块加载顺序:模块加载过程:(1)注册模块中定义的UObject,同时为每个类构造一个类默认对象(CDO,记录类的默认状态,作为模板用于子类实例创建)(2)调用模块的StartUpModule方法2、FEngineLoop::Init()1、检查Engine的配置文件找出使用了哪一个GameEngine类(UGame

  6. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  7. ruby - 确定 ruby​​ 脚本是否已经在运行 - 2

    有没有一种简单的方法可以判断ruby​​脚本是否已经在运行,然后适本地处理它?例如:我有一个名为really_long_script.rb的脚本。我让它每5分钟运行一次。当它运行时,我想看看之前运行的是否还在运行,然后停止第二个脚本的执行。有什么想法吗? 最佳答案 ps是一种非常糟糕的方法,并且可能会出现竞争条件。传统的Unix/Linux方法是将PID写入文件(通常在/var/run中)并在启动时检查该文件是否存在。例如pid文件位于/var/run/myscript.pid然后你会在运行程序之前检查它是否存在。有一些技巧可以避免

  8. ruby - ruby 脚本可以预编译成二进制文件吗? - 2

    我正在开发一个Ruby脚本,需要在没有Ruby解释器的情况下部署到系统上。它将需要在使用ELF格式的FreeBSD系统上运行。我知道有一个ruby​​2exe项目可以编译在Windows上运行的ruby​​脚本,但是在其他操作系统上这样做容易吗?甚至可能吗? 最佳答案 您是否检查过Rubinius或JRuby是否允许您预编译您的代码? 关于ruby-ruby脚本可以预编译成二进制文件吗?,我们在StackOverflow上找到一个类似的问题: https://

  9. ruby-on-rails - 如何用不同的用户运行nginx主进程 - 2

    A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(

  10. ruby - 使用 Capistrano 启动 sidekiq - 2

    我想用Capistrano启动sidekiq。下面是代码namespace:sidekiqdotask:startdorun"cd#{current_path}&&bundleexecsidekiq-c10-eproduction-Llog/sidekiq.log&"pcapture("psaux|grepsidekiq|awk'{print$2}'|sed-n1p").strip!endend它执行成功但sidekiq仍然没有在服务器上启动。输出:$capsidekiq:starttriggeringloadcallbacks*2014-06-0315:03:01executing`

随机推荐