<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>shutdown &#8211; Uroš Vovk</title>
	<atom:link href="https://www.urosvovk.com/tag/shutdown/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.urosvovk.com</link>
	<description>Uroš Vovk home page</description>
	<lastBuildDate>Mon, 20 Feb 2012 12:05:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Power on/off script for Xen Running VMs on XenServer</title>
		<link>https://www.urosvovk.com/power-onoff-script-for-xen-running-vms-on-xenserver/</link>
					<comments>https://www.urosvovk.com/power-onoff-script-for-xen-running-vms-on-xenserver/#comments</comments>
		
		<dc:creator><![CDATA[Uroš]]></dc:creator>
		<pubDate>Mon, 20 Feb 2012 12:05:57 +0000</pubDate>
				<category><![CDATA[Linux Stuff]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[power off]]></category>
		<category><![CDATA[power on]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shutdown]]></category>
		<category><![CDATA[Virtual machines]]></category>
		<category><![CDATA[VM]]></category>
		<category><![CDATA[xenserver]]></category>
		<guid isPermaLink="false">http://urosv.wordpress.com/?p=153</guid>

					<description><![CDATA[Days ago I needed a script to power on/off my runing VMs on XenServer&#8230; In my case I needed this to power off VMs when my UPS is running on batery and later to power on when the power comes back. I was looking for a script that can do the job, but after a &#8230; <a href="https://www.urosvovk.com/power-onoff-script-for-xen-running-vms-on-xenserver/" class="more-link">Continue reading <span class="screen-reader-text">Power on/off script for Xen Running VMs on XenServer</span></a>]]></description>
										<content:encoded><![CDATA[<p>Days ago I needed a script to power on/off my runing VMs on XenServer&#8230; In my case I needed this to power off VMs when my UPS is running on batery and later to power on when the power comes back.</p>
<p>I was looking for a script that can do the job, but after a while I didn&#8217;t find anything usefull&#8230; so I made my own script&#8230;<br />
<span id="more-153"></span><br />
My idea was to create a list of all running VMs and power off this VMs. Later, when I need to power on this VMs (from the list) I just call the same script again and it turn on all my VMs that are on this list.</p>
<p>Here is my script (shutdown_xen_vm.sh):</p>
<pre>#!/bin/bash

PROGRAMPATH="/root/scr/shutdown_vm"
if [ -f $PROGRAMPATH/vm-list.txt ]
then
   #$PROGRAMPATH/vm-list.txt exist
   echo "Power on:"
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} echo " - {}"
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} xe vm-start vm={}
   echo "Done"
   rm $PROGRAMPATH/vm-list.txt
else
   #$PROGRAMPATH/vm-list.txt do not exist
   xe vm-list power-state=running | grep name-label | awk '{print $4}' | tac | head -n -1 &gt; $PROGRAMPATH/vm-list.txt
   echo "Power off:"
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} echo " - {}"
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} xe vm-shutdown vm={}
   echo "Done"
   echo "Shutdown xen server..."
   shutdown -P now
fi</pre>
<h2>How it works:</h2>
<p>check if file vm-list.txt exist</p>
<pre>   if [ -f $PROGRAMPATH/vm-list.txt ]</pre>
<p>Just print a list of all VMs that were powered off</p>
<pre>   echo "Power on:"
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} echo " - {}"</pre>
<p>Start all VMs from the list and delete this list</p>
<pre>   cat $PROGRAMPATH/vm-list.txt | xargs -I{} xe vm-start vm={}
   echo "Done"
   rm $PROGRAMPATH/vm-list.txt</pre>
<p>Get all running VMs and write it to vm-list.txt</p>
<pre>xe vm-list power-state=running | grep name-label | awk '{print $4}' | tac | head -n -1 &gt; /root/scr/shutdown_vm/vm-list.txt</pre>
<p>Just print a list of all VMs that will be powered off</p>
<pre>   echo "Power off:"
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} echo " - {}"</pre>
<p>Power off VMs from the list and shutdown XenServer</p>
<pre>   
   cat $PROGRAMPATH/vm-list.txt | xargs -I{} xe vm-shutdown vm={}
   echo "Done"
   echo "Shutdown xen server..."
   shutdown -P now</pre>
<p>Now, to run this script you just need to do this:</p>
<pre>   
chmod +x shutdown_xen_vm.sh
./shutdown_xen_vm.sh</pre>
<p>&#8212;</p>
<p>I hope that this script will help you as it helped me :)</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.urosvovk.com/power-onoff-script-for-xen-running-vms-on-xenserver/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
