0

I'm running this script in terminal and getting desired results, but when I set a cron to run it from /root/somefolder/ every 5 min, it does not do what its supposed to do.

My root user crontab entry looks like this:

*/5 * * * * ~root/somedirectory/script.sh

The script is:

#!/bin/bash

## Variables ##
host="`/bin/hostname`";

        ## Limits ##
OneMin="1";
FiveMin="6";
FifteenMin="6";

     ## Mail IDs ##
To="someone@somedomain"; Fr="root@"$host;

     ## Load Averages ##

LA=(`uptime | grep -Eo '[0-9]+\.[0-9]+' | cut -d"." -f1`)

      ## Top Process List ##

tp=(`ps -ef | sort -nrk 3,3 | grep -E "(php|httpd)" | grep -v root | head -n30 | awk '{print $2}'`)

## Actions ##

if [ ${LA[0]} -ge $OneMin ]; then

      ## Send Mail ##
echo -e "From: $Fr
To: $To
Subject: *ALERT* - Current Load on '$host' Is High Load Averages Are: \n\n 1:Min\t5:Min\t15:Min \n
${LA[0]}\t${LA[1]}\t${LA[2]} \n\n List Of Processes That Were Killed \n" | sendmail -t

## Kill Top Pocesses ##
for i in $tp ; do kill -9 $i
     done

fi

Issues:

  1. All the recipients in $To variable don't get any alert, when script is run via cron even the if statement gets true, but when it is run in terminal, everyone gets an email.

  2. I tried to paste all email IDs directly in To: fields like this, cause I thought it's not reading $To variable. To: someone@somedomain instead of $To

But still none of the recipients gets any alert, and no actions seems to be performed.

Yaron
  • 4,289
Sollosa
  • 1,929
  • 4
  • 20
  • 38
  • 2
    I suppose space before #!/bin/bash are typos ? – Archemar Mar 13 '18 at 08:12
  • This is such a long example for a help site. Please try cutting it down to the minimum length that fails (or works). This will help to isolate the problem. One strategy would be to replace the first if section with writing to a temporary file instead. – Sparhawk Mar 13 '18 at 08:19
  • @Archemar yes typo just here. – Sollosa Mar 13 '18 at 08:27
  • Can you post crontab entry that runs this script as well? Is it root user's crontab? Is there anything that could explain the issue in /var/log/maillog? – yahol Mar 13 '18 at 09:31
  • @yahol yes its root user's cron. Crontab entry is /5 * * * ~root/somedirectory/script.sh. – Sollosa Mar 13 '18 at 11:48
  • @Sollosa change path to script in crontab to /root/somedirectory/script.sh. Root's home is under /root/, while regular users under /home/username/. ~root/ doesn't make sense, it would rather be ~/somedirectory/script.sh, but in this case i think it's better to set absolute path. – yahol Mar 13 '18 at 12:16
  • @yahol ok I got the reason for the issue. – Sollosa Mar 13 '18 at 12:45

2 Answers2

1

Try the following syntax for sendmail in your script:

#!/bin/bash

# some code

/usr/sbin/sendmail -t <<EOF
To: noc@example.com "$address1" "$address2"
Cc: support@example.com admin@example.com me@example.com
Subject: [Monitoring] $foo $bar at $host
From: root@example.com

Monitoring for example.com server loss of connectivity - hourly update:
---------------------------------------------------------------------------

$some
$more
$variables

EOF

You can embed variables in "heredoc" block.

Script name was monitor.sh. Entry i used in crontab, as root:

@hourly /root/monitor.sh

Issues related to sendmail or (failed) mail delivery can be checked in /var/log/maillog.

yahol
  • 168
1

It seems that shell commands need to be written with an absolute path, so sendmail should be /usr/bin/sendmail. I updated it and cron started working.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
Sollosa
  • 1,929
  • 4
  • 20
  • 38