Operating System - Linux
1753816 Members
8144 Online
108805 Solutions
New Discussion юеВ

script execution fails when executed from crontab

 
SOLVED
Go to solution
Sunny Jaisinghani
Trusted Contributor

script execution fails when executed from crontab

I have scheduled a script to run in crontab. the script monitors the load average and sends an email if load avg is above certain limit.

#!/bin/bash
avg=`uptime | awk '{print $8" " $9 " "$10 $11 $12 }' | tr -s , " "`
cur=`more /proc/loadavg | awk '{print $1}' | tr -d , | cut -d. -f1`
str="============================="
info="Curent $avg"

if [ $cur -ge 6 ]; then
info1="Server load is high presently"
touch /tmp/tmp.00
echo -e "$str\n$info\n$info1\n$str\n" >> /tmp/tmp.00
ps aux | head -1 >> /tmp/tmp.00
ps aux | sort -rn +2 | head -10 >> /tmp/tmp.00;
mail -s "Alert: Load Average for `hostname` on `date` " email1 < /tmp/tmp.00;
mail -s "Alert: Load Average for `hostname` on `date` " email2 < /tmp/tmp.00;
rm -f /tmp/tmp.00
else
#echo -e "$str\n$info\n$str"
exit 0;
fi


when i execute this script i don get any error.
However, when it runs through cron it throws an error.

./monitor_loadavg.sh: line 7: [: too many arguments

i don see anything wrong with line 7.

Please suggest if i am doing anything wrong

Thanks
SUnny
5 REPLIES 5
Matti_Kurkela
Honored Contributor
Solution

Re: script execution fails when executed from crontab

The problem is probably in the content of the $cur variable somehow.

On line 3, you use the "more" command to read /proc/loadavg. When you're running the script in your session, "more" can find the $TERM and $LINES environment variables that identify your terminal type and the number of lines in your terminal window.

But when running from cron, there is no terminal, and those variables won't exist. I guess "more" might produce some extra output (a notification message or something). You don't need it anyway: this version of line 3 of your script should work just the same, but without the uncertain behavior of "more":

cur=$(awk '{print $1}' < /proc/loadavg | tr -d, | cut -d. -f1)

mK
MK
Steven Schweda
Honored Contributor

Re: script execution fails when executed from crontab

> i don see anything wrong with line 7.

Probably because you don't see what "$cur"
is.

> if [ $cur -ge 6 ]; then

if [ "$cur" -ge 6 ]; then

But, if "cur" contains multiple tokens, then
you'll probably need to look more closely at
its definition.
Ivan Ferreira
Honored Contributor

Re: script execution fails when executed from crontab

Probably some of the commands for filling the cur variable is not working. This produces the error in line 7 as cur is empty or missformatted. Try adding the fill path to the more, awk, cut y tr commands.

Add an echo $cur on line 6 to debug the problem.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Dennis Handly
Acclaimed Contributor

Re: script execution fails when executed from crontab

>MK: cur=$(awk '{print $1}' < /proc/loadavg | tr -d, | cut -d. -f1)

Besides this change, you can probably replace tr(1) and cut(1) by more awk functions.
Sunny Jaisinghani
Trusted Contributor

Re: script execution fails when executed from crontab

Thanks Matti,

your suggestion worked for me.