How to verify if nginx is running or not?
Solution 1
You could use lsof
to see what application is listening on port 80:
sudo lsof -i TCP:80
Solution 2
This is probably system-dependent, but this is the simplest way I've found.
if [ -e /var/run/nginx.pid ]; then echo "nginx is running"; fi
That's the best solution for scripting.
Solution 3
If you are on mac machine and had installed nginx using
brew install nginx
then
brew services list
is the command for you. This will return a list of services installed via brew and their corresponding status.
Solution 4
The modern (systemctl
) way of doing it:
systemctl is-active nginx
You can use the exit value in your shell scripts as follows:
systemctl -q is-active nginx && echo "It is active, do something"
Solution 5
service nginx status
will work on a non-systemd based version.
On systemd based versions such as Ubuntu Linux 16.04 LTS and above, make use of the command below;
systemctl status nginx
Zameer Ansari
Updated on August 12, 2021Comments
-
Zameer Ansari over 2 years
After running an ASP.NET vNext project on my local machine I was trying to figure out how I can run it on nginx as it looks to be a recommended choice
Following jsinh's blog, I installed it using:
sudo apt-get update sudo apt-get install nginx -y
I was trying to understand whether it is working or not by using:
ifconfig eth0 | grep inet | awk '{ print $2}'
After running
sudo service nginx start sudo service nginx stop
However, the output is always the same:
How to verify if nginx is running or not?
-
Lex Li about 8 years@student that post is too old for a moving target such as ASP.NET Core, and it misses so many details.
-
Zameer Ansari about 8 yearsFine, at least it gives me a starting point. If you can show any newer alternative?
-
jb007 almost 7 yearssudo lsof -i was it for me
-
user541686 about 5 yearsDoesn't this fail if nginx has died abruptly?
-
Bob over 4 years@Mehrdad it surely does, there is absolutely no guarantee that something will clean up this pid file, so this "solution" is definetely unreliable.
-
Cole Tierney over 4 years@Sizzling Could it be listening on a different port or not running?
-
Captain Jack Sparrow almost 4 yearsThe question is about Ubuntu, not Mac users.
-
Dkyrii almost 4 yearsBut I didn't found the same question about Mac, when I had this problem. So it can be helpful for anyone like me. Also, this question already has the answer about mac users
-
Captain Jack Sparrow almost 4 yearsBut then why do you need to answer it again, if there's already a Mac answer?
-
Dkyrii almost 4 yearsBecause that answer can't be useful for bash scripts. I noticed that my answer is one for scripting.