當 rsync 更新 nginx.conf 時重新載入 nginx 配置

當 rsync 更新 nginx.conf 時重新載入 nginx 配置

我有兩個網頁伺服器,server1 和 server2。

在 server2 上有一個 rsync 命令,在 crontab 中安排,當我在 server1 上進行一些更改時,該命令會更新 /etc/nginx 資料夾:

*/5 * * * * rsync -avzhe ssh user@server1:/etc/nginx/ /etc/nginx/

我怎麼知道 rsync 已經改變了一些東西,所以我必須重新載入 nginx?

答案1

我找到了解決方案。

新增選項 -i 並刪除 -v,rsync 僅在檔案變更時輸出某些內容。所以我寫了這個腳本:

#!/bin/bash

RSYNC=$(rsync -aizhe ssh user@server1:/etc/nginx/ /etc/nginx/)

if [ $? -eq 0 ]; then
        if [ -n "${RSYNC}" ]; then
                /usr/sbin/nginx -s reload
                echo "reloaded."
        fi
else
        exit 1
fi

並將其放入 crontab 中。

相關內容