我正在尝试设置一封邮件,当邮件队列中有 100 封邮件时它会通知我。我不是 Linux 服务器人员,因此希望得到一些帮助。我有:
# get server hostname
hostname=`cat /etc/hostname`
current_mailq= "/var/qmail/bin/qmail-qstat"
# `postqueue -p | tail -n 1 | cut -d' ' -f5`
yourEmail="[email protected]"
if [ "$current_mailq" -gt "100" ]
then
echo "Mail queue problem - there is currently $current_mailq mails in queue. Please check it out." > check_email_queue_outgoing.txt
mail -s "$hostname - mail queue alert - there are $current_mailq emails in queue" "$yourEmail" < check_email_queue_outgoing.txt
else
echo "Mail queue is fine - there is currently $current_mailq mails in queue. Please check it out."
echo "Do nothing, situation is fine."
fi
它是我在 plesk 论坛上发现的。
当我运行 cron 作业时,它说
Output from command /home/xxxxxx/check_email_queue.sh ..
/home/xxxxxx/check_email_queue.sh: line 2:
: command not found
/home/xxxxxx/check_email_queue.sh: line 3:
: command not found
cat: /etc/hostname: No such file or directory
/home/xxxxxx/check_email_queue.sh: line 6:
: command not found
/home/xxxxxx/check_email_queue.sh: line 7: /var/qmail/bin/qmail-qstat
: No such file or directory
/home/xxxxxx/check_email_queue.sh: line 8:
: command not found
/home/xxxxxx/check_email_queue.sh: line 10:
: command not found
/home/xxxxxx/check_email_queue.sh: line 12:
: command not found
/home/xxxxxx/check_email_queue.sh: line 13:
: command not found
/home/xxxxxx/check_email_queue.sh: line 21: syntax error near unexpected token `fi'
/home/xxxxxx/check_email_queue.sh: line 21: `fi'
Mail handler 'limit-out' said: REPLY:554:5.7.0 Your message could not be sent. The user xxxxxx is not allowed to send email.
请问有什么想法吗?
编辑 ...
我把用户切换为 root,现在可以发送邮件了,但其他错误仍然存在 - 我无法找出邮件队列的大小,谢谢
答案1
尝试这个版本:
#!/bin/bash
hostname=`cat /etc/hostname`
current_mailq="`postqueue -p | tail -n 1 | cut -d' ' -f5`"
yourEmail="[email protected]"
echo "$current_mailq"
if (( "$current_mailq" >= "100" )) ; then
echo "Mail queue problem - there is currently $current_mailq mails in queue. Please check it out." > check_email_queue_outgoing.txt
mail -s "$hostname - mail queue alert - there are $current_mailq emails in queue" "$yourEmail" < check_email_queue_outgoing.txt
else
echo "Mail queue is fine - there is currently $current_mailq mails in queue. Please check it out."
echo "Do nothing, situation is fine."
fi
请注意,编写好的可移植 bash/sh/cmd 代码确实很困难。在我看来,最好采用Go 语言并为任何操作系统创建清晰的可移植程序。