Editing ExecuteInAllVEs

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
I found myself often faced with a need to run the same command in all VEs, e.g. ''apachectl restart'' to restart all webservers or ''dmesg | tail'' to see the latest news from everybody.
 
 
You can loop through all VEs and execute this this easily:
 
 
<code>
 
<code>
  for veid in `vzlist -Hoveid`; do vzctl exec $veid COMMAND; done
+
#!/bin/bash
</code>
+
CONFDIR="/etc/vz/conf"
 
+
if [ "$1" == "" ]; then
Knowing this, you can also save some typing later by making a simple utility out of it.
+
  echo "$0 -- Execute a command on all VEs"
I like to call this ''/usr/sbin/vzexec'' so I can later just say ''vzexec apachectl restart''
+
  echo ""
<code>
+
  echo "Example: vzexec service httpd restart"
  #!/bin/bash
+
  echo ""
  # vzexec -- execute a command on all VEs
+
  echo "Note that variables are expanded in the host's shell, and use '' to prevent this."
  if [ "$1" == "" ]; then
+
  echo "Example:  vzexec echo \$PWD    and  vzexec 'echo \$PWD'  are different."
    echo "$0 -- Execute a command on all VEs"
+
  exit
    echo ""
+
fi
    echo "Example: vzexec service httpd restart"
+
for veconf in $CONFDIR/*.conf ; do
    echo ""
+
  veid=`basename $veconf .conf`
    echo "Note that variables are expanded in the host's shell, and use ' ' to prevent this."
+
   if [ "$veid" == "0" ]; then continue; fi
    echo "Example:  vzexec echo \$PWD    and  vzexec 'echo \$PWD'  are different."
+
   vename=`grep ^NAME $veconf | head -1 | sed -e 's@NAME=@@' | sed -e 's@"@@g'`
    exit
+
  echo "*** VE $veid ($vename)"
   fi
+
  vzctl exec $veid $@
   for veid in `vzlist -Hoveid`; do
+
done
    echo "*** VE $veid"
 
    vzctl exec $veid $@
 
  done
 
 
</code>
 
</code>

Please note that all contributions to OpenVZ Virtuozzo Containers Wiki may be edited, altered, or removed by other contributors. If you don't want your writing to be edited mercilessly, then don't submit it here.
If you are going to add external links to an article, read the External links policy first!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)