【问题标题】:Shell script for cron to send an email if the command produces any output如果命令产生任何输出,则 cron 用于发送电子邮件的 Shell 脚本
【发布时间】:2017-01-04 19:07:28
【问题描述】:

我正在尝试设置 cron 以在脚本中运行以下命令:

find /path/to/folder -ctime -1 -type f

如果过去一天在特定文件夹中创建了任何文件,则发送邮件。到目前为止,我想出了这个:

#!/bin/bash

check=$(find /path/to/folder -ctime -1 -type f)
alert=mail@mail.com

if [ "$check" -eq 0 ]
then
    mail -s "files created" "$alert"
fi

问题是即使shell cheker 说没有错误,我也无法执行脚本。它给了我语法错误:文件意外结束

我已经在网上搜索了足够长的时间,发现 MAILTOMAILFROM 等其他解决方案或 this 等脚本无法正常工作。我也查看了this 线程,但似乎没有任何效果,如果我可以补充一点,这是针对 CentOS 6 的,以防它产生任何影响。

任何帮助将不胜感激。谢谢。

【问题讨论】:

标签: shell cron command


【解决方案1】:

对于仍然对答案感兴趣的任何人,这里就是。

#!/bin/bash

check=$(find /path/to/folder -ctime -1 -type f)
alert=mail@mail.com

if [[ "$check" ]]
then
    echo "$check" | mail -s "files created" "$alert"
fi

还要确保将 CRLF 换行符转换为 Unix 样式,如果它不起作用并且您不断收到无法解释的语法错误。

dos2unix /path/to/script

或关注this 链接。

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多