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" [email protected]
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.

3
  • Try to put simple brackets around the whole argument of the if condition.
    – user219962
    May 31, 2015 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? May 31, 2015 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. May 31, 2015 at 20:21

1 Answer 1

0
  1. You're trying to compare array to init value which is odd.
  2. You should print $load2 variable and see what contains.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .