
我有 crontab 和一個簡單的 bash 腳本,它經常向我發送電子郵件,其中包含日誌檔案中的 PHP、NGINX、MYSQL 錯誤。這是一個簡化的範例。
#/home/user/status.sh
[email protected]
PHP_ERROR=`tail -5 /var/log/php-fpm/error.log`
NGINX_ERROR=`tail -5 /var/log/nginx/error.log`
MYSQL_ERROR=`tail /var/log/mysqld.log`
DISK_SPACE=`df -h`
echo "
Today's, server report::
==================================
DISK_SPACE: $DISK_SPACE
---------------------------------
MEMORY_USAGE: $MEMORY_USAGE
-----------------------------------
NGINX ERROR: $NGINX_ERROR
-----------------------------------
PHP ERRORS: $PHP_ERROR
------------------------------------
MYSQL_ERRORS: $MYSQL_ERROR
-------------------------------------
" | mail -s "Server reports" $EMAIL
我知道這是一個非常基本的用法,但正如你所看到的,我試圖分離錯誤,但 html 標籤包括正在\n
工作的錯誤。
所以,我的問題是,是否可以使用 HTML 標籤來格式化文本,如果不能......那麼有什麼替代方案。
答案1
您可以使用郵件用戶端可以讀取的任何格式。為了更好地編碼您的訊息,請使用以下<<EOF
語法:
#/home/user/status.sh
[email protected]
PHP_ERROR=`tail -5 /var/log/php-fpm/error.log`
NGINX_ERROR=`tail -5 /var/log/nginx/error.log`
MYSQL_ERROR=`tail /var/log/mysqld.log`
DISK_SPACE=`df -h`
mail -s "Server reports" $EMAIL <<EOF
Today's, server report::
==================================
DISK_SPACE: $DISK_SPACE
----------------------------------
MEMORY_USAGE: $MEMORY_USAGE
----------------------------------
NGINX ERROR: $NGINX_ERROR
----------------------------------
PHP ERRORS: $PHP_ERROR
----------------------------------
MYSQL_ERRORS: $MYSQL_ERROR
----------------------------------
EOF