124

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?

Dharmang
  • 3,018
  • 35
  • 38
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219

12 Answers12

230

Looking at the requirement you have, the below command shall help:

service nginx status
Captain Jack Sparrow
  • 971
  • 1
  • 11
  • 28
34

You could use lsof to see what application is listening on port 80:

sudo lsof -i TCP:80
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35
26

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.

aleclarson
  • 18,087
  • 14
  • 64
  • 91
  • 2
    Doesn't this fail if nginx has died abruptly? – user541686 Apr 22 '19 at 20:20
  • 2
    @Mehrdad it surely does, there is absolutely no guarantee that something will clean up this pid file, so this "solution" is definetely unreliable. – Bob Aug 23 '19 at 17:17
20

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

Gurmeet Singh
  • 774
  • 8
  • 21
17

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"
Selcuk
  • 57,004
  • 12
  • 102
  • 110
13

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
nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
11

For Mac users

I found out one more way: You can check if /usr/local/var/run/nginx.pid exists. If it is - nginx is running. Useful way for scripting.

Example:

if [ -f /usr/local/var/run/nginx.pid ]; then
   echo "Nginx is running"

fi
Dkyrii
  • 111
  • 1
  • 3
4

None of the above answers worked for me so let me share my experience. I am running nginx in a docker container that has a port mapping (hostPort:containerPort) - 80:80 The above answers are giving me strange console output. Only the good old 'nmap' is working flawlessly even catching the nginx version. The command working for me is:

 nmap -sV localhost -p 80

We are doing nmap using the -ServiceVersion switch on the localhost and port: 80. It works great for me.

Andhi Irawan
  • 456
  • 8
  • 15
saferJo
  • 497
  • 1
  • 5
  • 16
4

Can also use the following code to check the nginx status:

   sudo /etc/init.d/nginx status
Biranchi
  • 16,120
  • 23
  • 124
  • 161
4

The other way to see it in windows command line :

tasklist /fi "imagename eq nginx.exe"

INFO: No tasks are running which match the specified criteria.

if there is a running nginx you will see them

Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
1

Not sure which guide you are following, but if you check out this page,

https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-14-04-lts

It uses another command

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' 

and also indicates what result is expected.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
0

Running the sudo nginx again gives you the following error Error Image This indicates that the nginx is running.

or the simple thing you can do is, check if the system that depends on nginx to run like the webapp you might be running, is it working when you hit that particular port

Prajval Singh
  • 501
  • 3
  • 9