Difference between revisions of "ExecuteInAllVEs"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
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. This simple utility is just a loop around ''vzctl exec'' but I find it very handy.
+
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.
  
 +
At its core is just this simple tidbit: a for loop around vzlist.
 +
<code>
 +
  for veid in `vzlist -Hoveid`; do vzctl exec $veid COMMAND; done
 +
</code>
 +
 +
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''
 
<code>
 
<code>
 
   #!/bin/bash
 
   #!/bin/bash

Revision as of 15:22, 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.

At its core is just this simple tidbit: a for loop around vzlist.

 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