0

The goal of the script below is to test nginx configuration and if successful it will then restart the service as shown.

#!/bin/bash
echo "checking nginx config..."
if sudo nginx -t | grep -q 'successful'; then
        echo "restaring nginx..."
        sudo systemctl restart nginx
fi

When this script is executed it prints

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

However, I wonder why the statement inside the if-statement does not run even if the result text contains "successful"

quarks
  • 33,478
  • 73
  • 290
  • 513
  • maybe the string is printed on stderr? And why do you check for the string, wouldn't it be simpler to just `if sudo nginx -t; then` ? – KamilCuk Jun 15 '19 at 15:55

1 Answers1

1

Replace

nginx -t

with

nginx -t 2>&1

to wirte its stderr to stdout.

Cyrus
  • 84,225
  • 14
  • 89
  • 153