bucur89
bucur89

Reputation: 170

Cron with sendmail incompatibility

I am trying to create a cron that every 2 days send the current space usage in an email to myself.

I can run the script perfectly and the email is sent but when I put it in the cronlist doesn't send the email.

The cron is active because I created a file that outputs the current date to a file and put it in the cronlist and it has been working.

This is my email script

#!/bin/bash
TOTAL=$(df / -h | grep / | awk '{ print $2}' )
USED=$(df / -h | grep / | awk '{ print $3}' )
AVAILABLE=$(df / -h | grep / | awk '{ print $4}' )
PERCENTAGE=$(df / -h | grep / | awk '{ print $5}' )
EMAILSUBJECT="Server 79.XXX.XXX.XXX"
sendmail "[email protected]" <<END
Subject: $EMAILSUBJECT

Disk usage on Server 79.XXX.XXX.XXX
Total space:       $TOTAL
Used space:        $USED
Available space:   $AVAILABLE
Percentage:        $PERCENTAGE
END

This is the test output for the crontab -l

[root@server ~]# crontab -l
0 * * * * /usr/sbin/ntpdate ntp.blacknight.ie
0 21 * * * /bin/nice -n 10 /usr/local/directadmin/plugins/awstats/hooks/cgi-bin/updateall.sh
* * * * * bash /root/checkspace.sh
* * * * * bash /root/test.sh

The test.sh is the one that outputs the date, and the file is being filled.

The checkspace.sh sends the mail, and if I type bash /root/checkspace.sh in the command line the email is sent, but the cron doesn't work.

Is there any kind of incompatibility between the cron and the mail??

Upvotes: 0

Views: 132

Answers (1)

fedorqui
fedorqui

Reputation: 289495

As commented above, there are many things that are likely to cause the problem. Probably, the need to set the full path in the commands you use.

From https://stackoverflow.com/tags/crontab/info

Common problems uncovered this way:

foo: Command not found or just foo: not found.

Most likely $PATH is set in your .bashrc or similar interactive init file. Try specifying all commands by full path (or put source ~/.bashrc at the start of the script you're trying to run).


Also, I want to indicate another way to do this block of code:

TOTAL=$(df / -h | grep / | awk '{ print $2}' )
USED=$(df / -h | grep / | awk '{ print $3}' )
AVAILABLE=$(df / -h | grep / | awk '{ print $4}' )
PERCENTAGE=$(df / -h | grep / | awk '{ print $5}' )

Note that you are calling df -h / four times, while one suffices:

read _ TOTAL USED AVAILABLE PERCENTAGE _ < <(df -hP / | tail -1)

Where tail -1 is used to remove the header. sed '/Filesystem/d' could also be used.

Or if you want to use a line for each one of them, do:

TOTAL=$(df / -hP | awk '/\// { print $2}' )
                                ^^^^
                                filter lines containing /

Note the usage of -P in df, that stands for Portability (or --portability, "use the POSIX output format"), used to prevent lines being split if they are too long. Thanks to Etan Reisner for this.

Upvotes: 3

Related Questions