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"

1
  • 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 ? Commented Jun 15, 2019 at 15:55

1 Answer 1

1

Replace

nginx -t 

with

nginx -t 2>&1 

to wirte its stderr to stdout.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.