13

I am trying to fix a problem with nginx and think that there is an nginx process running on my machine. When I run this command I get this output.

$ ps aux -P | grep nginx
1000      3947  0.0  0.0  13596   932 pts/0    S+   14:05   0:00 grep --color=auto nginx

But if I try to kill processes 1000, 3947, 13596 or 932 I get errors like:

bash: kill: (1000) - No such process

What's going on? How do I find and kill the nginx process?

bernie2436
  • 6,655

10 Answers10

9

I usually find pgrep more convenient than ps | grep. You might also want to look at killall if the end goal is blasting a bunch of processes by name.

  • 1
    +1 I prefer it, too. I guess you don't see it around much because it relies on having /proc, so it's less portable than a vanilla ps | grep. – Joseph R. Aug 13 '14 at 22:16
6

ps aux -P shows these columns:

$ ps aux -P | head -1
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

Only the 2nd column is the pid.

Warning: don't try to kill random pids.

pid 3947 was the grep process that ended when you got your prompt back, so there truly is no such process.

If there was an nginx process, you would have seen it in the grep output. Conclusion: nginx is not running on your machine.

glenn jackman
  • 85,964
5

A common trick to avoid the grep command itself from showing up in ps output is to put the first character in a character class so that it won't match its literal self. I.e.,

$ ps aux | grep [n]ginx

This way the grep pattern will match "nginx" but not the literal "[n]ginx" that shows up in the ps output.

Steve
  • 199
2

But if I try to kill processes 1000, 3947, 13596 or 932 I get errors like [...]

in the output of ps, 1000 is the user ID of the process (grep in that case), 3947 is the PID of grep, and the other numbers are status parameter values and not at all PIDs.

If you want to see all processes by name, then use something like

ps -lfC nginx

caveat: you have to know the exact name, otherwise ps doesn't return anything. If you don't know the exact name, then use pgrep.

countermode
  • 7,533
  • 5
  • 31
  • 58
2

Very often you must use sudo for managing nginx process. Prepend all the commands below with sudo if you got 'Operation is not permitted' error. Although this question is tagged as 'ps' I'd like to mention the other opportunities.

In case you just want to stop nginx I would recommend using:

service nginx stop

But if your purpose is to find 'pid' itself (to send a signal for example) I'd recommend looking for nginx.pid. Once nginx is started it puts it's pid into this file.

nginx can be controlled with signals. The process ID of the master process is written to the file /usr/local/nginx/logs/nginx.pid by default

It might be somewhere else, though.

In case you have compiled nginx by youself you could have specified pid-path as well.

--pid-path=path — sets the name of an nginx.pid file that will store the process ID of the main process. After installation, the file name can always be changed in the nginx.conf configuration file using the pid directive. By default the file is named prefix/logs/nginx.pid

Otherwise you can specify path to pid file explicitly in nginx.conf.

Read about Nginx Control

So, to send a signal just type:

kill `cat /path/to/nginx.pid`

Or

kill $(cat /path/to/nginx.pid)

Read about killing the process by pid file.

Personally, writing script or deployment instructions I wouldn't entrust stopping or searching nginx pid on pgrep and especially on grep, though they are applicable on the local machine. But in this case I would shorten stopping to pkill (process kill):

pkill nginx
Nick Roz
  • 165
1

on most nginx installs you can find the pid on the file system: cat /var/run/nginx.pid

bhurlow
  • 111
0

To kill all the nginx processes you could do:

kill $(ps aux | grep 'nginx' | awk '{print $2}')
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Inc33
  • 101
0

Run this command:

sudo kill -9 $(ps aux | grep 'nginx' | awk '{print $2}')
  • This looks a bit dangerous, you might end up killing many more processes than you intend to. Plus, using SIGKILL (-9) when it's not absolutely necessary is not a good idea. – lgeorget Aug 09 '18 at 15:52
0

try using this command

ps -ax | grep nginx

You will be getting the first process id with master process mentioned and then use that id to kill it.

mnille
  • 529
0

First, identify the master process:

$ ps -ef |grep nginx
root     1394258       1  0 13:19 ?        00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
www-data 1394265 1394258  0 13:19 ?        00:00:00 nginx: worker process

Then, to shut down operations, use the QUIT signal to "gracefully" kill the master process:

$ sudo kill -QUIT 1394258