quarks
quarks

Reputation: 35282

Test and restart service with bash

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"

Upvotes: 0

Views: 826

Answers (1)

Cyrus
Cyrus

Reputation: 88654

Replace

nginx -t

with

nginx -t 2>&1

to wirte its stderr to stdout.

Upvotes: 1

Related Questions