Cronjob运行每小时和电子邮件结果



让我告诉我第一次做了什么

#update.sh
#!/bin/bash
/usr/bin/freshclam
maldet -b -a /home

另一个脚本

#doandmail.sh
./update.sh > mail.txt
SUBJECT="Shell Script"
EMAIL="myemail@gmail.com"
EMAILMESSAGE="mail.txt"
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

当我使用./doandmail.sh运行doandmail.sh时,会发送一封带有结果的电子邮件。我在cron @hourly /custom/doandmail.sh中添加了这一行,每小时我都会收到一封空白电子邮件。

我完全是新手,需要你的建议来解决。

我要说问题出在/update.sh>mail.txt

Cron可以用路径来搞笑——让这些路径成为绝对路径,然后再试一次。

解释器指令之前的行-#!行之前-是错误的,但可能不是您的问题。#!仅作为可执行文件的前两个字符是特殊的,它标识了应该打开它的程序(在本例中为/bin/bash)。shell倾向于通过默认为自己来解释脚本,但这并不可靠,尤其是对于非sh脚本。

第二,http://www.talisman.org/~erlkonig/documents/commandname扩展被认为是有害的

所以在/custom/update

#!/bin/bash
#  update
/usr/bin/freshclam
maldet -b -a /home

然后运行:chmod +x /custom/update

./doandmail:中

#!/bin/bash
#  doandmail
SUBJECT="Shell Script"      # these don't need to be uppercase
EMAIL="myemail@gmail.com"   # ...though it doesn't hurt anything
EMAILMESSAGE="mail.txt"     # usually only exported variable are upper.
/custom/update | /bin/mail -s "$SUBJECT" "$EMAIL"   # no need for a tmp file.

然后:chmod +x doandmail

当您的crontab运行时,它将不会有您所想的相同目录,甚至不会有您可能期望的相同环境,除非您明确设置它们。它很可能在./update上坏掉。。。CCD_ 12中的行。因此产生了上述CCD_ 13。

在您的crontab中:

@hourly /custom/doandmail

最新更新