Share an nginx restart script

Source: Internet
Author: User
Nginx is a super stable server and generally does not need to be restarted due to overload. The purpose of the restart is to modify the configuration file and load it. At the beginning, I was using the most direct Restart Method killall-9nginx; datanginxsbinnginx if the machine is slow, kill the process can not be killed in an instant, and then execute it again. This nginx is a super stable server and generally does not need to be restarted due to overload. The purpose of the restart is to modify the configuration file and load it.

At the beginning, I used the most direct restart method.

KillAll-9 nginx;/data/nginx/sbin/nginx

If the machine is slow, the kill process can't be killed in an instant, and you can execute it again. This restart method is not particularly secure. If the configuration is incorrect, the restart will fail. You need to modify the configuration file and start it again, which takes a little time. However, for http, which is still not very strict at present, this time will not cause too much loss, as long as it is not made at a critical moment. If you want to use this restart method, I suggest you test it first.

Later, I saw a more amazing restart on nginx.net.

Kill-HUP $ p Id($ Pid is the process number of the nginx master process)

I usually use this

Kill-hup' Cat/Data/nginx/logs/nginx. pid'

The advantage of this method is to implement "smooth restart". In ps-aux, we can see that nginx starts a new process and the old process still provides services. After a while, the old process service is automatically shut down after it ends, and the new process continues to serve. However, this method also has some disadvantages. If the configuration file is incorrect or the resource conflict occurs, the restart fails, but nginx does not have any prompts! This will often find that the modified configuration file does not take effect, and it is difficult to find the problem.

So, I finally fixed the problem and made an nginx. sh, nginx of this version. sh still does not solve the problem of resource conflicts in kill-HUP, but solves the problem of configuration files. Resource conflicts, such as port 80 being occupied and the log file directory is not created, I will try again.

#! /Bin/sh

BASE_DIR = '/data /'

$ {BASE_DIR} nginx/sbin/nginx-t-c $ {BASE_DIR} nginx/conf/nginx. conf >&$ {BASE_DIR} nginx/logs/nginx. start

Info = 'cat $ {BASE_DIR} nginx/logs/nginx. start'

If ['echo $ info | Grep-C syntax is OK '-eq 1]; then

If ['ps aux | grep nginx | grep-c master' = 1]; then
Kill-HUP 'cat $ {BASE_DIR} nginx/logs/nginx. pid'
Echo OK
E LsE
Killall-9 nginx
Sleep1
$ {BASE_DIR} nginx/sbin/nginx
Fi

Else

Echo ######## error :########
Cat $ {BASE_DIR} nginx/logs/nginx. start

Fi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.