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:
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.
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.
if
section with writing to a temporary file instead. – Sparhawk Mar 13 '18 at 08:19crontab
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/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