2

I'm using Mac. I used ps -ef | grep nginx which returned the following result.

$ ps -ef | grep nginx
    0 74428     1   0  3:44PM ??         0:00.01 nginx: master process nginx -c /usr/local/etc/nginx/nginx.conf  
   -2 74483 74428   0  3:47PM ??         0:00.00 nginx: worker process  
  501 75545 75489   0  4:31PM ttys003    0:00.00 grep nginx

Does it mean the Nginx is running? Is there any other reliable way to check if Nginx is running?

Additionally, does nginx restart automatically every time the machine restarts?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • To exclude the `grep nginx` process, try adding a character class to your pattern: `ps -ef | grep '[n]ginx'`. Looks like it's running. To double check, find out if a process is listening on port 80: `sudo lsof -i TCP:80` – Cole Tierney Dec 11 '22 at 20:26
  • `sudo lsof -i TCP:80` returns `COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME` and `WeChat 4475 softtimur 258u IPv6 0x7dde4063cf92003b 0t0 TCP ties-mbp:59103->162.62.97.147:http (CLOSE_WAIT)`. Does it mean Nginx is running or not? – SoftTimur Jan 18 '23 at 04:59
  • Have you tried any of the solutions from https://stackoverflow.com/questions/1821886/check-if-mac-process-is-running-using-bash-by-process-name ? – Agent Biscutt Jan 18 '23 at 06:12
  • You could try `lsof` https://stackoverflow.com/questions/35220654/how-to-verify-if-nginx-is-running-or-not/35231900#35231900 – Cole Tierney Jan 25 '23 at 02:21

5 Answers5

2

If you installed nginx with Homebrew, you can use:

brew services info nginx

You will get a similar output:

nginx (homebrew.mxcl.nginx)
Running: ✔
Loaded: ✔
Schedulable: ✘
User: user
PID: 78476
fedterzi
  • 1,105
  • 7
  • 17
1

Question 1: Is Nginx running? Yes, the output you get indicates that Nginx is currently running on your mac. The first line in the output shows the master process with PID 74428 and the second line shows a worker process with PID 74483.

Question 2: Other ways to find out if Nginx is running.

  1. Use the command nginx -t which will test the configuration files for syntax errors. If the configuration is valid, it will display the message "nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful."
  2. Using the command netstat -an | grep 80 to check if a process is listening on port 80, which is the default port for Nginx.
  3. Using the command lsof -i :80 to check if any process is using port 80.
  4. By using the command sudo nginx -s status that will give you the status of Nginx.
  5. Third party tools like Nagios (open-source).

Question 3: Does nginx restart automatically every time the machine restarts?

By default, Nginx does not automatically start at boot time on macOS, so it will not automatically restart after a machine restart. You can use launchd or launchctl to set up Nginx as a service so that it starts automatically on boot.

If you wish to do so. 2 ways.

  1. Create a launchd plist file and use launchctl command to load it. Create a plist file called nginx.plist in the directory /Library/LaunchDaemons/ with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>nginx</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/nginx</string>
        <string>-g</string>
        <string>daemon off;</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>UserName</key>
    <string>_www</string>
    <key>GroupName</key>
    <string>_www</string>
    <key>StandardErrorPath</key>
    <string>/var/log/nginx/error.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/nginx/access.log</string>
  </dict>
</plist>

Yoiu might have to change the path inside the element. Load the plist file using the launchctl command :

sudo launchctl load /Library/LaunchDaemons/nginx.plist

Use the command "launchctl list | grep nginx" to verify that Nginx is running.

To remove the plist file and stop Nginx from starting automatically on boot:

sudo rm /Library/LaunchDaemons/nginx.plist
  1. If you have used Homebrew to install Nginx you could you the following commands
brew services info nginx // status answer to question 2
brew services start nginx  // start Nginx
brew services enable nginx // configure Nginx to start automatically on boot
brew services list // verify that Nginx is set to automatically run on boot
brew services stop nginx // stop Nginx
brew services disable nginx // remove the service so not to run Nginx on boot
Smit Gabani
  • 240
  • 2
  • 6
0

The output you provided shows that nginx is indeed running on your Mac.

  • The first line with the user "root" (0) and process ID "74428" is the master process for nginx.
  • The second line with the process ID "74483" is a worker process.
  • The last line is the "grep nginx" command you used to find the nginx processes.
Kaleb Fenley
  • 216
  • 1
  • 5
0
  1. ps aux | grep nginx
  2. nginx -t to test for config files
  3. sudo lsof -i :80. (check for ngnix in the list using grep command)
  4. sudo service nginx status
hypermails
  • 726
  • 1
  • 10
  • 28
0
alias nginx-status="\
  test -s /var/run/nginx.pid \
  && echo '▶ Nginx is running' || echo '■ Nginx is stopped'"

-s checks to see if the PID file "exists and has a size greater than zero"

anthumchris
  • 8,245
  • 2
  • 28
  • 53