<?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>GIS and Web Tricks &#187; sed</title>
	<atom:link href="http://www.bernawebdesign.ch/byteblog/tagged/sed/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bernawebdesign.ch/byteblog</link>
	<description>Thougts and tricks on GIS, web development and FOSS</description>
	<lastBuildDate>Tue, 31 Jan 2012 23:52:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Activating error 503 from a shell script</title>
		<link>http://www.bernawebdesign.ch/byteblog/2009/07/14/activating-error-503-from-a-shell-script/</link>
		<comments>http://www.bernawebdesign.ch/byteblog/2009/07/14/activating-error-503-from-a-shell-script/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 12:19:15 +0000</pubDate>
		<dc:creator>marco</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell scripting]]></category>

		<guid isPermaLink="false">http://www.bernawebdesign.ch/byteblog/?p=60</guid>
		<description><![CDATA[During deployment of a new version of a website you might want to have your server returning an error 503 (temporarily unavailable). here a solution]]></description>
			<content:encoded><![CDATA[<p>During deployment of a new version of a website you might want to have your server returning an error 503 (temporarily unavailable). You can easily do this by pointing your web folder to a php page that contains this:<span id="more-60"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//server_down.php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP/1.1 503 Service Unavailable&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;We are deploying a new version of NoSoapNoBubbles, the server is going to be down for a minute.&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>if your webfolder is linked to another folder with a symlink (as I explained in my previous post <a href="http://www.bernawebdesign.ch/byteblog/2009/07/10/symfony-project-on-bluehost-shared-hosting/">symfony project on bluehost shared hosting</a>) then you can just remove the link and add a new one pointing to the folder containing server_down.php. Remember to rename server_down.php index.php or to create an .htaccess file containing</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">DirectoryIndex server_down.php</pre></div></div>

<p> so that server_down.php is shown as directory index.</p>
<p>This solution is more than fine, but I needed to access some files during deployment so I decided to use a combination of the approach above and .htaccess. When the web directory (of our dev site) is being wiped out, then I redirect using the link, when the directory is filled but I&#8217;m running DB tasks I redirect using .htaccess.</p>
<p>Since our deploy tasks are fully automated (happen on svn commit) here how to edit the .htaccess file using SED (stream editor).<br />
Activate redirection:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$#&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> Invalid number of arguments, must be <span style="color: #000000;">1</span>. <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #007800;">HTACCESSPATH</span>=<span style="color: #007800;">$1</span> <span style="color: #666666; font-style: italic;">#path to the .htaccess file</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span>  <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccess <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s|#*RewriteCond %{REQUEST_URI} !server_down.php\$|RewriteCond %{REQUEST_URI} !server_down.php\$|'</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #000000; font-weight: bold;">&gt;</span> TMPFILE <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> TMPFILE <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s|#*RewriteCond %{REQUEST_URI} !always_available.php\$|RewriteCond %{REQUEST_URI} !always_available.php\$|'</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #000000; font-weight: bold;">&gt;</span> TMPFILE <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> TMPFILE <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s|#*RewriteRule ^(.*)\$ /server_down.php \[L\]|RewriteRule ^(.*)\$ /server_down.php [L]|'</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #000000; font-weight: bold;">&gt;</span> TMPFILE <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> TMPFILE <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccess<span style="color: #000000; font-weight: bold;">&lt;/</span>code<span style="color: #000000; font-weight: bold;">&gt;</span>
Deactivate redirection:<span style="color: #000000; font-weight: bold;">&lt;</span>code<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;$#&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> Invalid number of arguments, must be <span style="color: #000000;">1</span>. <span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">2</span>
  <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #007800;">HTACCESSPATH</span>=<span style="color: #007800;">$1</span>
<span style="color: #c20cb9; font-weight: bold;">cp</span>  <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccess <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s|RewriteCond %{REQUEST_URI} !server_down.php\$|#RewriteCond %{REQUEST_URI} !server_down.php\$|'</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #000000; font-weight: bold;">&gt;</span> TMPFILE <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> TMPFILE <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s|RewriteCond %{REQUEST_URI} !always_available.php\$|#RewriteCond %{REQUEST_URI} !always_available.php\$|'</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #000000; font-weight: bold;">&gt;</span> TMPFILE <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> TMPFILE <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">'s|RewriteRule ^(.*)\$ /server_down.php \[L\]|#RewriteRule ^(.*)\$ /server_down.php [L]|'</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #000000; font-weight: bold;">&gt;</span> TMPFILE <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> TMPFILE <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP
<span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccessTMP <span style="color: #007800;">$HTACCESSPATH</span><span style="color: #000000; font-weight: bold;">/</span>.htaccess</pre></div></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bernawebdesign.ch/byteblog/2009/07/14/activating-error-503-from-a-shell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

