Score:1

Cronjob is not running properly but work while run command (./)

in flag

I have this script for file 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

}

In crontab, I have this line:

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

I have restarted crontab, but nothing is happening.

If I run with the command manually, like ./test8.sh, then I receive an email.

Why is this not working from crontab?

ar flag
Try adding the `/full/path/to/sendmail` in your script.
hr flag
@user68186 agreed - iirc sendmail is typically in `/usr/sbin`, which is not part of cron's default `PATH` - see for example this related answer [Can't mount another server in crontab with 16.04](https://askubuntu.com/a/919512/178692). As well, your script defines a function SEND_EMAIL - but does not appear to actually call it.
muru avatar
us flag
You don't actually call the `SEND_EMAIL` function from what I can see.
Score:1
in flag

I solved it by adding the path /sbin/sendmail on the script

#!/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
Score:0
ve flag

Please check by giving the time delay as follow, if it might be the main cause. if still, this will not work you have to check the script.

For edit the 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

And try. Usually, it should work.

Terrance avatar
id flag
You do understand that this means to run the job at 1 minute past every hour? So the job would only run once per hour. Every minute is either `*/1` or just `*`.
Brijesh Sondarva avatar
ve flag
Once I have faced the same issue and it has been resolved by adding time delay...
Terrance avatar
id flag
There's nothing wrong with adding a delay. The problem with your statement is that they would have to wait 1 hour before the cron job ran again if they were testing it out. When testing a cron job you would want it to run as often as possible so that you can test it. Your time here would mean I could only test it 24 times a day. Maybe make it delay for 3 minutes by making it `*/3` or every 2 minutes `*/2`.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.