Cronjob 无法正常运行,但在运行命令(./)时可以工作

Cronjob 无法正常运行,但在运行命令(./)时可以工作

我有这个文件的脚本testfile.sh

SEND_EMAIL(){

TO="[email protected]"
CC_MAIL="[email protected]"

sendmail $TO,$CC_MAIL <<EOF
From: [email protected]
To: $TO
Cc: $CC_MAIL
Subject:Report
Content-Type: text/html;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th,table {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
        empty-cells: show;
}
-->
</style>
</head>
<body>
Hi Team,<br><br>

Fyi, IT Operation repot<br>
<br>

<table cellpadding="0" cellspacing="0" border="1" bordercolor="black"  style="wi
dth: 500px">
        <tr bgcolor="silver"  align="center" style="font-weight:bold">
                <td>REPORTED_BY</td>
                <td>TICKET_ID</td>
                <td>HELP_TOPIC</td>
                <td>SUBJECT</td>
                <td>CREATION_DATE</td>
                <td>REOPENED</td>
                <td>EST_DUEDATE</td>
                <td>UPDATED</td>
                <td>PRIORITY</td>
                <td>USERNAME</td>
        </tr>
</table>
<br>
<br>

Regards,<br>

IT Team

</body>
</html>
EOF

}

在 crontab 中,我有以下一行:

* * * * * /bin/sh /opt/testfile.sh

我已经重新启动了 crontab,但什么也没有发生。

如果我手动运行该命令,例如./test8.sh,那么我会收到一封电子邮件。

为什么这不能从 crontab 运行?

答案1

/sbin/sendmail我通过在脚本中添加路径解决了这个问题

#!/bin/bash

SEND_EMAIL(){

TO="[email protected]"
CC_MAIL="[email protected]"

/sbin/sendmail $TO,$CC_MAIL <<EOF
From: [email protected]
To: $TO
Cc: $CC_MAIL
Subject:Report
Content-Type: text/html;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th,table {
        font-family: Verdana, Arial, Helvetica, sans-serif;
        font-size: 12px;
        empty-cells: show;
}
-->
</style>
</head>
<body>
Hi Team,<br><br>

Fyi, IT Operation repot<br>
<br>

<table cellpadding="0" cellspacing="0" border="1" bordercolor="black"  style="wi
dth: 500px">
        <tr bgcolor="silver"  align="center" style="font-weight:bold">
                <td>REPORTED_BY</td>
                <td>TICKET_ID</td>
                <td>HELP_TOPIC</td>
                <td>SUBJECT</td>
                <td>CREATION_DATE</td>
                <td>REOPENED</td>
                <td>EST_DUEDATE</td>
                <td>UPDATED</td>
                <td>PRIORITY</td>
                <td>USERNAME</td>
        </tr>
</table>
<br>
<br>

Regards,<br>

IT Team

</body>
</html>
EOF


}
SEND_EMAIL

答案2

请通过提供如下时间延迟来检查它是否是主要原因。如果仍然无效,您必须检查脚本。

编辑 crontab

$ crontab -e
1 * * * * /root/clearcache.sh

Example of job definition:

.---------------- minute (0 - 59)

|  .------------- hour (0 - 23)

|  |  .---------- day of month (1 - 31)

|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR 

sun,mon,tue,wed,thu,fri,sat

|  |  |  |  |

*  *  *  *  * user-name  command to be executed

$ sudo systemctl restart cron.service

然后尝试一下。通常情况下,它应该会起作用。

相关内容