How to verify if nginx is running or not?

165,600

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.

enter image description here

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
Share:
165,600
Zameer Ansari
Author by

Zameer Ansari

Updated on August 12, 2021

Comments

  • Zameer Ansari
    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:

    Nginx status

    How to verify if nginx is running or not?

  • Lex Li
    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
    Zameer Ansari about 8 years
    Fine, at least it gives me a starting point. If you can show any newer alternative?
  • jb007
    jb007 almost 7 years
    sudo lsof -i was it for me
  • user541686
    user541686 about 5 years
    Doesn't this fail if nginx has died abruptly?
  • Bob
    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
    Cole Tierney over 4 years
    @Sizzling Could it be listening on a different port or not running?
  • Captain Jack Sparrow
    Captain Jack Sparrow almost 4 years
    The question is about Ubuntu, not Mac users.
  • Dkyrii
    Dkyrii almost 4 years
    But 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
    Captain Jack Sparrow almost 4 years
    But then why do you need to answer it again, if there's already a Mac answer?
  • Dkyrii
    Dkyrii almost 4 years
    Because that answer can't be useful for bash scripts. I noticed that my answer is one for scripting.