Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write nginx Service Startup Program by shell

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to write shell nginx service startup program, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.

Nginx installed with the source package cannot be operated and controlled using "service nginx start" or "/ etc/init.d/nginx start", so the following service control script is written.

The options are:

Start starts stop stops reload reloads restart restarts status status test check configuration file

Script one

Create a script file and add execution permissions

Touch / etc/init.d/nginx chmod + x / etc/init.d/nginx

Write script content

#! / bin/bash # chkconfig:-85 15 # description: Nginx server control script # processname: nginx # config file: / usr/local/nginx/conf/nginx.conf # pid file: / usr/local/nginx/logs/nginx.pid # eastmoney public tools # version: v1.0.0 # create by XuHoo, 2016-9-14 # # source function library. / etc/rc.d/init.d/functions NGINX_NAME= "nginx" NGINX_PROG= "/ usr/local/sbin/nginx" NGINX_PID_FILE= "/ usr/local/nginx/logs/nginx.pid" NGINX_CONF_FILE= "/ usr/local/nginx/conf/nginx.conf" NGINX_LOCK_FILE= "/ var/lock/subsys/nginx.lock" # check current user ["$USER"! = "root"] & & Exit 1 start () {status if [[$?-eq 0]] Then echo $"Nginx (PID $(cat $NGINX_PID_FILE)) already started." return 1 fi echo-n $"Starting $NGINX_NAME:" daemon $NGINX_PROG-c $NGINX_CONF_FILE retval=$? echo [$retval-eq 0] & & touch $NGINX_LOCK_FILE return $retval} stop () {status if [[$?-eq 1]] Then echo "Nginx server already stopped." return 1 fi echo-n $"Stoping $NGINX_NAME:" killproc $NGINX_PROG retval=$? echo [$retval-eq 0] & & rm-f $NGINX_LOCK_FILE return $retval} restart () {stop sleep 1 start retval=$? return $retval} reload () {echo-n $"Reloading $NGINX_NAME:" Killproc $NGINX_PROG-HUP retval=$? echo return $retval} status () {netstat-anpt | grep "/ nginx" | awk'{print $6}'& > / dev/null if [[$?-eq 0]] Then if [[- f $NGINX_LOCK_FILE]]; then return 0 else return 1 fi fi return 1} _ status () {status if [[$?-eq 0]] Then state= `netstat-anpt | grep "/ nginx" | awk'{print $6} '`echo $"Nginx server status is: $state" else echo "Nginx server is not running" fi} test () {$NGINX_PROG-t-c $NGINX_CONF_FILE retval=$? return $retval} case "$1" in start) start; stop) stop;; reload) reload Restart) restart;; status) _ status;; test) test;; *) echo "Usage: {start | stop | reload | restart | status | test}" exit 1 esac

Add a script to the system service and set up boot

Chkconfig-add nginx chkconfig-level 3 nginx on

Script two

[root@localhost ~] # cd / usr/local/nginx/conf/ [root@localhost conf] # ls fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default

Back up the master configuration file

[root@localhost conf] # cp nginx.conf nginx.conf.origin [root@localhost conf] # vim nginx.conf remove # pid logs/nginx.pid The previous # [root@localhost conf] # netstat-anpt | grep 80 tcp 00 0.0.0.0 anpt 80 0.0.0.0 tcp * LISTEN 19108/nginx [root@localhost conf] # kill-3 19108 [root@localhost conf] # netstat-anpt | grep 80 [root@localhost conf] # nginx [root@localhost conf] # netstat-anpt | grep 80 tcp 00 0.0.0.0.0anpt 80 0.0.0.0 : * LISTEN 19864/nginx [root@localhost conf] # cd.. / logs/ [root@localhost logs] # ls access.log error.log nginx.pid [root@localhost logs] # cat nginx.pid 19864

Edit and write service script

The options available for [root@localhost ~] # vim / etc/init.d/nginx are: start start stop stop reload reload restart restart status status test check configuration file #! / bin/bash # chkconfig: 2345 99 20 # description:Nginx Server Control Scripts shell PROG= "/ usr/local/nginx / sbin/nginx "PIDF=" / usr/local/nginx/logs/nginx.pid "case" $1 "in start) if [- f $PIDF] Then echo "Nginx is running …" Else $PROG fi;; stop) if [- f $PIDF]; then kill-3 $(cat $PIDF) rm-f $PIDF else echo "Nginx is stopping …" Fi;; restart) $0 stop $0 start;; reload) if [- f $PIDF]; then kill-1 $(cat $PIDF) else echo "Nginx is stopping … Reload "fi;; status) if [- f $PIDF]; then echo" Nginx is running "else echo" Nginx stop "fi;; *) echo" Usage: $0 (start | stop | restart | reload | status) "exit 1 esac exit 0

Script file and add execute permission

[root@localhost ~] # chmod + x / etc/init.d/nginx add script to system service and set boot [root@localhost ~] # chkconfig-- add nginx as service startup item [root@localhost ~] # chkconfig-- list nginx nginx 0: close 1: close 2: enable 3: enable 5: enable 6: close [root@localhost ~] # chkconfig-- level 3 nginx on tests whether the script can execute [root@localhost ~] # service nginx start Nginx is running. [root@localhost] # service nginx restart [root@localhost ~] # service nginx stop [root@localhost ~] # service nginx stop Nginx is stopping … [root@localhost ~] # service nginx start [root@localhost ~] # service nginx status Nginx is running the above content is how to write nginx service startup program for shell. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 241

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report