Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux logrotate未运行预旋转脚本_Linux_Bash_Cron_Logrotate - Fatal编程技术网

Linux logrotate未运行预旋转脚本

Linux logrotate未运行预旋转脚本,linux,bash,cron,logrotate,Linux,Bash,Cron,Logrotate,我写了一个脚本来解析我的nginx日志,并通过电子邮件将结果发送给我,我将其添加到我的nginx logrotate脚本中,但它不起作用 /var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 0640 www-data adm sharedscr

我写了一个脚本来解析我的nginx日志,并通过电子邮件将结果发送给我,我将其添加到我的nginx logrotate脚本中,但它不起作用

/var/log/nginx/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                /root/bin/nginx-parse.sh
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}
脚本位于
prerotate
部分,当我以root用户的身份调用它时,它就会工作,我认为logrotate就是以root用户的身份运行的

我可以看到,当我运行
logrotate-d-f/etc/logrotate.d/nginx
时,它不会向我发送任何电子邮件

我应该从这里去哪里

alert-mail.sh.sh:

#!/usr/bin/python
import smtplib
import sys
from email.mime.text import MIMEText

server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login("me@example.com", "secret")

# Create message
msg = MIMEText(sys.argv[2])
msg['Subject'] = sys.argv[1] 
msg['From'] = "me@example.com" 
msg['To'] = "me@example.com"

server.sendmail(msg["From"], [msg["To"]], msg.as_string())
server.quit()
nginx-parse.sh

#!/bin/bash
# For parsing of the nginx logfile and emailing to myself

rm /root/bin/nginx_email.txt
printf "NGINX STATUS CODES\n\n" >> /root/bin/nginx_email.txt
cat /var/log/nginx/access.log | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -rn >> /root/bin/nginx_email.txt
printf "\n\n404 URLS\n\n" >> /root/bin/nginx_email.txt
awk '($9 ~ /404/)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -rn  >> /root/bin/nginx_email.txt
printf "\n\n502 URLS\n\n" >> /root/bin/nginx_email.txt
awk '($9 ~ /502/)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -r >> /root/bin/nginx_email.txt
printf "\n\nMOST ACCESSES URLS\n\n" >> /root/bin/nginx_email.txt
awk -F\" '{print $2}' /var/log/nginx/access.log | awk '{print $2}' | sort | uniq -c | sort -rg | head -n 50 >> /root/bin/nginx_email.txt

#Sending the email using the python script I wrote
alert-mail "NGINX PARSING" "$(cat /root/bin/nginx_email.txt)"

我知道/root/bin/nginx-parse.sh脚本会向您发送电子邮件。从logrotate运行时,请确保mail等命令的二进制文件具有完整路径(例如/usr/bin/mail,而不仅仅是mail)。你剧本的内容是什么?另外,使用-v标志运行logrotate:logrotate-d-v-f/etc/logrotate.d/nginx

我发布了我的脚本。编辑完帖子后,我尝试将完整路径添加到警报邮件脚本中,这是一个python脚本,也在我的
/root/bin
中,记住nginx解析脚本可以工作…并发送邮件…当logrotate运行扫描时,它不会触发你粘贴logrotate-v-f/etc/logrotate.d/nginx的日志?您是否有机会看到类似的内容:/root/bin/nginx-parse.sh:第15行:警报邮件:命令现在无法运行。我不确定到底是哪件事修复了它,但它正在工作。谢谢