Difference between revisions of "ExecuteInAllVEs"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
(program code, no wording)
 
 
(8 intermediate revisions by the same user not shown)
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.
#!/bin/bash
+
 
CONFDIR="/etc/vz/conf"
+
You can loop through all VEs and execute this this easily:
if [ "$1" == "" ]; then
+
<code>
  echo "$0 -- Execute a command on all VEs"
+
  for veid in `vzlist -Hoveid`; do vzctl exec $veid COMMAND; done
  echo ""
+
</code>
  echo "Example: vzexec service httpd restart"
+
 
  echo ""
+
Knowing this, you can also save some typing later by making a simple utility out of it.
  echo "Note that variables are expanded in the host's shell, and use '' to prevent this."
+
I like to call this ''/usr/sbin/vzexec'' so I can later just say ''vzexec apachectl restart''
  echo "Example:  vzexec echo \$PWD    and  vzexec 'echo \$PWD'  are different."
+
<code>
  exit
+
  #!/bin/bash
fi
+
  # vzexec -- execute a command on all VEs
for veconf in $CONFDIR/*.conf ; do
+
  if [ "$1" == "" ]; then
  veid=`basename $veconf .conf`
+
    echo "$0 -- Execute a command on all VEs"
  if [ "$veid" == "0" ]; then continue; fi
+
    echo ""
  vename=`grep ^NAME $veconf | head -1 | sed -e 's@NAME=@@' | sed -e 's@"@@g'`
+
    echo "Example: vzexec service httpd restart"
  echo "*** VE $veid ($vename)"
+
    echo ""
  vzctl exec $veid $@
+
    echo "Note that variables are expanded in the host's shell, and use ' ' to prevent this."
done
+
    echo "Example:  vzexec echo \$PWD    and  vzexec 'echo \$PWD'  are different."
}}}
+
    exit
 +
  fi
 +
  for veid in `vzlist -Hoveid`; do
 +
    echo "*** VE $veid"
 +
    vzctl exec $veid $@
 +
  done
 +
</code>

Latest revision as of 15:23, 6 January 2008

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:

 for veid in `vzlist -Hoveid`; do vzctl exec $veid COMMAND; done 

Knowing this, you can also save some typing later by making a simple utility out of it. I like to call this /usr/sbin/vzexec so I can later just say vzexec apachectl restart

 #!/bin/bash
 # vzexec -- execute a command on all VEs
 if [ "$1" == "" ]; then
   echo "$0 -- Execute a command on all VEs"
   echo ""
   echo "Example: vzexec service httpd restart"
   echo ""
   echo "Note that variables are expanded in the host's shell, and use ' ' to prevent this."
   echo "Example:   vzexec echo \$PWD    and   vzexec 'echo \$PWD'   are different."
   exit
 fi
 for veid in `vzlist -Hoveid`; do
   echo "*** VE $veid"
   vzctl exec $veid $@
 done