1. #! /bin/sh
    2. # chkconfig: 2345 55 25
    3. # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
    4. # run 'update-rc.d -f nginx defaults', or use the appropriate command on your
    5. # distro. For CentOS/Redhat run: 'chkconfig --add nginx'
    6. ### BEGIN INIT INFO
    7. # Provides: nginx
    8. # Required-Start: $all
    9. # Required-Stop: $all
    10. # Default-Start: 2 3 4 5
    11. # Default-Stop: 0 1 6
    12. # Short-Description: starts the nginx web server
    13. # Description: starts nginx using start-stop-daemon
    14. ### END INIT INFO
    15. # Author: licess
    16. # website: http://lnmp.org
    17. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    18. NAME=nginx
    19. NGINX_BIN=/usr/local/qif/nginx/sbin/$NAME
    20. CONFIGFILE=/usr/local/qif/nginx/conf/$NAME.conf
    21. PIDFILE=/usr/local/qif/nginx/logs/$NAME.pid
    22. case "$1" in
    23. start)
    24. echo -n "Starting $NAME... "
    25. if netstat -tnpl | grep -q nginx;then
    26. echo "$NAME (pid `pidof $NAME`) already running."
    27. exit 1
    28. fi
    29. $NGINX_BIN -c $CONFIGFILE
    30. if [ "$?" != 0 ] ; then
    31. echo " failed"
    32. exit 1
    33. else
    34. echo " done"
    35. fi
    36. ;;
    37. stop)
    38. echo -n "Stoping $NAME... "
    39. if ! netstat -tnpl | grep -q nginx; then
    40. echo "$NAME is not running."
    41. exit 1
    42. fi
    43. $NGINX_BIN -s stop
    44. if [ "$?" != 0 ] ; then
    45. echo " failed. Use force-quit"
    46. exit 1
    47. else
    48. echo " done"
    49. fi
    50. ;;
    51. status)
    52. if netstat -tnpl | grep -q nginx; then
    53. PID=`pidof nginx`
    54. echo "$NAME (pid $PID) is running..."
    55. else
    56. echo "$NAME is stopped"
    57. exit 0
    58. fi
    59. ;;
    60. force-quit)
    61. echo -n "Terminating $NAME... "
    62. if ! netstat -tnpl | grep -q nginx; then
    63. echo "$NAME is not running."
    64. exit 1
    65. fi
    66. kill `pidof $NAME`
    67. if [ "$?" != 0 ] ; then
    68. echo " failed"
    69. exit 1
    70. else
    71. echo " done"
    72. fi
    73. ;;
    74. restart)
    75. $0 stop
    76. sleep 1
    77. $0 start
    78. ;;
    79. reload)
    80. echo -n "Reload service $NAME... "
    81. if netstat -tnpl | grep -q nginx; then
    82. $NGINX_BIN -s reload
    83. echo " done"
    84. else
    85. echo "$NAME is not running, can't reload."
    86. exit 1
    87. fi
    88. ;;
    89. configtest)
    90. echo -n "Test $NAME configure files... "
    91. $NGINX_BIN -t
    92. ;;
    93. *)
    94. echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
    95. exit 1
    96. ;;
    97. esac
    1. # 执行的脚本
    2. touch /etc/init.d/nginx #用管理员权限,在/etc/init.d/目录下,创建文件 nginx
    3. vim nginx # 使用vim工具,将上面的脚本信息贴到文件中
    4. update-rc.d -f nginx defaults #用管理员权限,将文件加入到update-rc.d的脚本下

    参考文档:

    1. https://www.cnblogs.com/wkun/p/3798816.html
    2. https://blog.csdn.net/qq_41345773/article/details/88855178?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control