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
/proc
, so it's less portable than a vanillaps | grep
. – Joseph R. Aug 13 '14 at 22:16