1

I'm trying to work through a script to email me a notification if the load is too high on our server. I found a good one but it's giving me and error when I run it, and I can't see why.

Running the code below gives the error:

line 13: syntax error near unexpected token `fi'

I thought I had to laid out correctly though. Thanks!

#!/bin/bash
THR=10
MAIL="[email protected]"
VAR=`uptime|awk -F, '{print 4ドル}'|awk '{print 3ドル}'`
OUT=`echo "$VAR $THR" | awk '{if (1ドル > 2ドル) print "yes"; else print "no"}'`
if [ "$VAR" == "" ]
then
 # it's within the first 24 hours of uptime
 VAR=`uptime|awk -F, '{print 3ドル}'|awk '{print 3ドル}'`
 OUT=`echo "$VAR $THR" | awk '{if (1ドル > 2ドル) print "yes"; else print "no"}'`
fi
if [ "$OUT" == "yes" ]
then
 echo "The current load $VAR is greater than the threshold $THR" | mail $MAIL
 -s "Server Load Alert"
 echo "Alert generated because $VAR is greater than $THR"
else
 echo "No alert as $VAR > $THR"
fi
echo "load = $VAR"
Tomasz Jakub Rup
10.7k7 gold badges51 silver badges49 bronze badges
asked Dec 4, 2012 at 9:03
4
  • execute with the flag -vx in your shebang line and check exactly what the error is! #!/bin/bash -vx Commented Dec 4, 2012 at 9:10
  • Hmm the whitespace was giving me an error so I've removed that. Now I'm getting an unexpected end of file Commented Dec 4, 2012 at 9:20
  • Trying running it in debug mode, #/bin/bash --debug --verbose file.sh Commented Dec 4, 2012 at 9:27
  • Same error, is this supposed to put the contents of the error into a file? Commented Dec 4, 2012 at 9:33

2 Answers 2

2

Sorry, no offence, but your bash style is terrible!

Here's a better version:

#!/bin/bash
thr=10
mail="[email protected]"
read var _ < /proc/loadavg
if (( $(bc -l <<< "$var>$thr") )); then
 echo "The current load $var is greater than the threshold $thr" | mail "$mail" -s "Server Load Alert"
 echo "Alert generated because $var is greater than $thr"
else
 echo "No alert as $var <= $thr"
fi
echo "load = $var"

The changes are the following:

  • Use lower case variable names, as upper case variable names are considered bad bash practice.
  • Don't parse the output of the command uptime using millions of pipes, subshells and awks because it's inefficient, the same information is obtained directly from the file /proc/loadavg, with a read builtin.
  • Don't use awk to test for inequalities, use bc, it's more efficient (and you don't need a variable $OUT at all).
  • No backticks! Use the $(...) construct instead (easier to read, to nest, and better bash practice).

I haven't tested the script, just corrected yours as I read it. Please tell me if it works for you.

answered Dec 4, 2012 at 9:45

8 Comments

I got the following output when running. root@server[~]# bash /scripts/load_check.sh : command not foundcheck.sh: line 2: : command not foundcheck.sh: line 5: : No such file or directory: line 6: /proc/loadavg : command not foundcheck.sh: line 7: /scripts/load_check.sh: line 15: syntax error: unexpected end of file
@AdamG What are the scripts load_check.sh and foundcheck.sh? Could you edit your OP and include them there?
load_check.sh is this script that I'm excuting. I have no idea what foundcheck.sh is. I've got plenty of other scripts running on the server, I'm not sure why this one is causing such a headache.
@AdamG Ah it's about the script check.sh, not foundcheck.sh... Could you please check that the file /proc/loadavg exists by issuing cat /proc/loadavg. Could you also check that you have bash installed and check its version by issuing bash --version.
There is definitely no script called check.sh and I'm not running a command for that. cat /proc/loadavg gives me 0.32 0.29 0.27 1/235 14795 which looks correct? Lastly it's Bash 3.2.25(1)-release.
|
0
#!/bin/bash
THR=10
MAIL="[email protected]"
VAR=`uptime|awk -F, '{print 4ドル}'|awk '{print 3ドル}'`
OUT=`echo "$VAR $THR" | awk '{if (1ドル > 2ドル) print "yes"; else print "no"}'`
if [ "$VAR" == "" ]
then
# it's within the first 24 hours of uptime
VAR=`uptime|awk -F, '{print 3ドル}'|awk '{print 3ドル}'`
OUT=`echo "$VAR $THR" | awk '{if (1ドル > 2ドル) print "yes"; else print "no"}'`
fi
if [ "$OUT" == "yes" ]
then
echo "The current load $VAR is greater than the threshold $THR" | mail $MAIL -s "Server Load Alert"
echo "Alert generated because $VAR is greater than $THR"
else
echo "No alert as $VAR > $THR"
fi
echo "load = $VAR"

This works for me. I changed so that "mail $MAIL" and -s "Server Load Alert" keeps on the same row.

answered Dec 4, 2012 at 9:24

3 Comments

I'm still getting an Unexpected end of file. Are there any changes I need to make on the server for it to be more forgiving?
Execute the script but swap #!/bin/bash with #!/bin/bash -x and post the output
I got the following output when running. root@server[~]# bash /scripts/load_check.sh : command not foundcheck.sh: line 2: : command not foundcheck.sh: line 5: line 22: syntax error: unexpected end of file

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.