0

I must first of all shamefully admit that I unfortunately know nothing about scripts... I am simply trying to use one that seems to fit my goal.

I am running a VPS node with OpenVZ, and I need a script which will automatically restart the VPS abusing the server load by using the specific command "vzctl restart SERVERID".

However, I'm getting nowhere unfortunately and the script returns errors when launched.

The original script is as follows:

#! /bin/bash
export PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
maxload="20"; # put here the max loadavg permitted
cat /dev/null > loads.txt;
vzlist -o ctid,laverage > loads.txt;
cat loads.txt | while read line; do
vm=$(echo ${line:0:5});
load=$(echo ${line} | cut -d'/' -f3);
load2=$(echo ${load} | cut -d'.' -f1);
if [ $load2 -gt $maxload ]
    then
    echo "restarting $vm - $load";
    vzctl restart $vm;
    echo "$vm - $load" | mail -s "$vm restarted for overload" hostingstudio.net@gmail.com
fi
done

If I launch it, I get this error:

antiload.sh: line 10: [: too many arguments

I researched a bit on the Internet, and I then tried isolating the variable on line 10 with quotes, thus changing the code like this:

if [ "$load2" -gt $maxload ]

but I still get an error, as follows:

antiload.sh: line 10: [[: CTID LAVERAGE: syntax error in expression (error token is "LAVERAGE")

Please, can someone help me to debug this script so that I will be able to use it?

Thanks for your attention, time and help.

  • Try to put simple brackets around the whole argument of the if condition. –  May 31 '15 at 17:46
  • 2
    It looks like `vlist`'s output isn't in the format the script expects. Can you include a sample of what `vzlist -o ctid,laverage` actually prints on your system? – Gordon Davisson May 31 '15 at 17:58
  • That said, you should add this line to the start of your file: `set -x` and post the output then. That's how you debug some bash. – moebius_eye May 31 '15 at 20:21

1 Answers1

0
  1. You're trying to compare array to init value which is odd.
  2. You should print $load2 variable and see what contains.
Peycho Dimitrov
  • 1,118
  • 9
  • 10