国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Crontab和Shell腳本切割Nginx日志使用詳解

瀏覽:4日期:2023-08-07 20:15:49
目錄一、配置 Crontab 定時任務二、編寫 shell 腳本三、遇到的問題一、配置 Crontab 定時任務配置文件路徑:/var/spool/cron/root添加定時任務59 23 * * * /root/app/shell/nginx-log-slice.sh

配置解釋:在 每天的23時59分 執行 /root/app/shell/nginx-log-slice.sh 腳本。

配置其他自定義時間可以參考:Linux強大的定時任務-Crontab,有詳細的配置說明。

二、編寫 shell 腳本文件路徑:/root/app/shell/nginx-log-slice.shnginx-log-slice.sh 完整代碼#!/bin/bash. /etc/profilesource /etc/profilesource ~/.bash_profilePATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin# 當發生錯誤時中止腳本set -ebase='/usr/local/nginx/logs/'day='`date '+%Y%m%d'`'curDir='${base}${day}'accessLog=${base}access.logerrorLog=${base}error.logms='`date '+%s'`'# 判斷以當天日期為名的文件夾是否存在,存在打印輸出,不存在就創建if [ -d ${curDir} ];then echo '${curDir} 文件夾存在' else echo '${curDir} 文件夾不存在' mkdir ${curDir}fi# 復制當前的 access.log 和 error.log 日志文件,文件名加上時間戳,并存入以當天日期為名的文件夾\cp -rf ${accessLog} ${curDir}/access-${ms}.log\cp -rf ${errorLog} ${curDir}/error-${ms}.log# 復制完成后清空原本的 access.log 和 error.log 日志文件> ${accessLog}> ${errorLog}# 輸出成功或失敗的信息if [ $? == 0 ]; then echo 'nginx slice 成功 (${curDir}/access-${ms}.log)' else echo 'nginx slice 失敗 (${curDir}/access-${ms}.log)'fi三、遇到的問題

問題:crontab 定時任務執行 shell 腳本時,可能遇到這種報錯:/bin/sh: /root/app/shell/nginx-log-slice.sh: Permission denied,這就說明 shell 腳本權限不足。解決:執行以下命令為 nginx-log-slice.sh 文件授權。

chmod 777 /root/app/shell/nginx-log-slice.sh

使用 ll 命令查看文件權限

cd /root/app/shellll

在授權前會輸出:

[root@VM-8-12-centos shell]# ll總用量 8-rwxrwxrwx 1 root root 404 10月 15 2021 test.sh-rw-r--r-- 1 root root 610 10月 9 2022 nginx-log-slice.sh

在授權后會輸出:

[root@VM-8-12-centos shell]# ll總用量 8-rwxrwxrwx 1 root root 404 10月 15 2021 test.sh-rwxrwxrwx 1 root root 610 10月 9 2022 nginx-log-slice.sh

一切就緒后建議執行 nginx -s reload 重啟 Nginx 服務

以上就是Crontab和Shell腳本切割Nginx日志的詳細內容,更多關于Crontab Shell切割Nginx的資料請關注好吧啦網其它相關文章!

標簽: Nginx
主站蜘蛛池模板: 忻城县| 前郭尔| 北宁市| 衡水市| 盱眙县| 肥东县| 彭阳县| 肥东县| 华坪县| 黄浦区| 牙克石市| 昌宁县| 钟祥市| 紫金县| 澄迈县| 方城县| 富裕县| 宾川县| 上虞市| 西和县| 娄底市| 客服| 溧水县| 光泽县| 德钦县| 绵竹市| 靖边县| 六枝特区| 乡宁县| 海南省| 长武县| 自治县| 蒲江县| 开平市| 呼和浩特市| 溆浦县| 商南县| 彭山县| 桃园县| 南京市| 兴山县|