Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Update: After the changes from the first answer I got some more error codes.

: not found: line 23: {

esxidown.sh: line 24: redir error Insufficient arguments.

: not found: line 26:

esxidown.sh: line 53: syntax error: unexpected word (expecting "do")

I am new to shellscripting so I barely understand whats going on but unfortunately not enough to debug it properly.

I got a problem with a script I am trying to run.

The situation is: Me having a UPS and wanting to get a script which shuts down my VM's and afterwards shuts down the Host if the UPS starts running.

I already found a script that should savely shut down the VM's and then shut down the host.

But I get the following error: syntax error: unexpected "("

I tried a few things but nothing worked.

I hope you can help me.

Script without personal changes:

#!/bin/sh
# ESXi 5.1 host automated shutdown script

# these are the VM IDs to shutdown in the order specified
# use the SSH shell, run "vim-cmd vmsvc/getallvms" to get ID numbers
# specify IDs separated by a space
SERVERIDS=$(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}')

# New variable to allow script testing, assuming the vim commands all work to issue shutdowns
# can be "0" or "1"
TEST=0

# script waits WAIT_TRYS times, WAIT_TIME seconds each time
# number of times to wait for a VM to shutdown cleanly before forcing power off.
WAIT_TRYS=20

# how long to wait in seconds each time for a VM to shutdown.
WAIT_TIME=10

# ------ DON'T CHANGE BELOW THIS LINE ------

validate_shutdown()
{
    vim-cmd vmsvc/power.getstate $SRVID | grep -i "off" > /dev/null 2<&1
    STATUS=$?

    if [ $STATUS -ne 0 ]; then
if [ $TRY -lt $WAIT_TRYS ]; then
        # if the vm is not off, wait for it to shut down
        TRY=$((TRY + 1))
        echo "Waiting for guest VM ID $SRVID to shutdown (attempt #$TRY)..."
        sleep $WAIT_TIME
        validate_shutdown
    else
        # force power off and wait a little (you could use vmsvc/power.suspend here instead)
        echo "Unable to gracefully shutdown guest VM ID $SRVID... forcing power off."
        if [ $TEST -eq 0 ]; then
vim-cmd vmsvc/power.off $SRVID
        fi
sleep $WAIT_TIME
    fi
fi
}

# enter maintenance mode immediately
echo "Entering maintenance mode..."
if [ $TEST -eq 0 ]; then
esxcli system maintenanceMode set -e true -t 0 &
fi

# read each line as a server ID and shutdown/poweroff
for SRVID in $SERVERIDS
do
TRY=0

vim-cmd vmsvc/power.getstate $SRVID | grep -i "off\|Suspended" > /dev/null 2<&1
STATUS=$?

if [ $STATUS -ne 0 ]; then
echo "Attempting shutdown of guest VM ID $SRVID..."
    if [ $TEST -eq 0 ]; then
vim-cmd vmsvc/power.shutdown $SRVID
    fi
validate_shutdown
else
echo "Guest VM ID $SRVID already off..."
fi
done

# guest vm shutdown complete
echo "Guest VM shutdown complete..."

# shutdown the ESXi host
echo "Shutting down ESXi host after 10 seconds..."
if [ $TEST -eq 0 ]; then
esxcli system shutdown poweroff -d 10 -r "Automated ESXi host shutdown - esxidown.sh"
fi

# exit maintenance mode immediately before server has a chance to shutdown/power off
# NOTE: it is possible for this to fail, leaving the server in maintenance mode on reboot!
echo "Exiting maintenance mode..."
if [ $TEST -eq 0 ]; then
esxcli system maintenanceMode set -e false -t 0
fi

# exit the session
exit
share|improve this question
    
What are you trying to do? SERVERIDS=1 2 3 4(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}') –  devnull Apr 23 at 11:01
    
This is a script I got from this article: nojokeit.com/2012/11/… 1 2 3 4 are the Server ID's from my virtual machines. I think in this line the script is trying to say that 1 2 3 4 are the ID's and where to find them. So the script can check them. But I am really new to this stuff so I am not sure. –  Timo Apr 23 at 12:24
    
I suggest you go do some reading on how to write shell scripts. If you want to know what's going on inside of there do: sh -vx /path/to/script –  mikeserv Apr 23 at 12:26
    
@Timo The script pasted in your question seems different from the one in the link mentioned. It seems that you have introduced typos. –  devnull Apr 23 at 12:28
    
I edited my VM ID's. Original: SERVERIDS=$(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ [.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}') My Version with VM ID's: SERVERIDS=1 2 3 4(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ [.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}') Did I understand this wrong? –  Timo Apr 23 at 12:36

1 Answer 1

This doesn't do what you think:

SERVERIDS=1 2 3 4(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}')

This will set SERVERIDS=1 for that command only and then runs the "command" 2 with the arguments 3 and 4(...), the latter of which is not valid syntax. Using more quotes, you probably want something like this:

SERVERIDS="1 2 3 4 $(vim-cmd vmsvc/getallvms | sed -e '1d' -e 's/ \[.*$//' | awk '$1 ~ /^[0-9]+$/ {print $1}')"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.