'Nginx Invalid PID number

I issued a nginx -s stop and after that I got this error when trying to reload it.

[error]: invalid PID number "" in "/var/run/nginx.pid"

That /var/run/nginx/pid file is empty atm.

What do I need to do to fix it?



Solution 1:[1]

in my case I solved this by starting the service.

sudo /etc/init.d/nginx start

The command above will start the service in Debian/Ubuntu. It will issue an error if there is any problem (like Apache listening in the same port)

After that nginx -s reload will work like a charm

Solution 2:[2]

This will clear out the issue on ubuntu 16.04 and above

sudo service nginx stop    

you may need to remove the pid file nginx.pid whose location may be defined in file /etc/nginx/nginx.conf look for line like

cat /etc/nginx/nginx.conf | grep pid  #  see if pid file is defined

this line may live in file /etc/nginx/nginx.conf

pid /run/nginx.pid;  #  in file /etc/nginx/nginx.conf

if pid file does exist then remove it now

ls -la  /var/run/nginx/pid  #  this file may live elsewhere
ls -la  /run/nginx.pid  #  on Ubuntu 16.04+

after the pid file has been removed lets launch nginx

sudo service nginx start

ps -eaf|grep nginx        #  confirm its running

sudo nginx -t && sudo nginx -s reload   #  confirm config is OK

#      typical output
#  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#  nginx: configuration file /etc/nginx/nginx.conf test is successful


sudo service nginx stop   #   issue stop

ps -eaf|grep nginx        #   confirm it actually stopped

now sanity has been restored and you are free to launch at will

Solution 3:[3]

In the latest version(1.2.0) that I downloaded there is no "-s start" option, it will say nginx: invalid option: "-s start"

You can start nginx by

sudo /etc/nginx/sbin/nginx

The server will be started and then there wont be any Invalid pid number errors.

Solution 4:[4]

To avoid downtime with restarting nginx,

ps aux | grep nginx 
PID of nginx master process

echo PID > /var/run/nginx.pid
nginx -s reload

Solution 5:[5]

In my case nginx was stopped (crashed I assume). Solved the issue by:

service nginx status
nginx stop/waiting

service nginx start
nginx start/running, process 3535

Then nginx -s reload worked like a charm.

I am using nginx/1.8.0 on trusty.

Solution 6:[6]

On CentOS 7 I done it with this:

 sudo systemctl start nginx
 #Then check all things are OK
 sudo systemctl status -l nginx

Solution 7:[7]

This happens if the nginx process was stopped manually or was killed.

Check if the process is still running:

sudo lsof -nP -iTCP:<port> | grep LISTEN

I am on mac, and I reinstall the nginx with:

brew reinstall nginx

Then start the service using brew:

brew services start nginx

Solution 8:[8]

For anyone who still has issues, in my case, there was an apache2 server that was running. You can try debugging what went wrong in your nginx machine by executing this command - systemctl status nginx This gave me an insight that the port was already in us by apache2 server. so you can do sudo service apache2 stop and then do sudo service nginx start.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrián Deccico
Solution 2 danronmoon
Solution 3 Botz3000
Solution 4 b3nky
Solution 5 ramonamis
Solution 6
Solution 7 Vishrant
Solution 8 KKM