<?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>Fernando Ipar &#187; shell</title>
	<atom:link href="http://fernandoipar.com/tag/shell/feed/" rel="self" type="application/rss+xml" />
	<link>http://fernandoipar.com</link>
	<description>I love mankind! Its people I can&#039;t stand!</description>
	<lastBuildDate>Tue, 06 Dec 2011 01:01:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Piping data to multiple processes</title>
		<link>http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/</link>
		<comments>http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 05:00:06 +0000</pubDate>
		<dc:creator>fernando</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://fernandoipar.com/?p=216</guid>
		<description><![CDATA[Here&#8217;s a simple shell script to stream data to multiple processes. It has many applications, but the reason I wrote it is to stream the same data to multiple netcat processes on remote machines. Here&#8217;s the code: #!/bin/bash usage() { cat &#60;&#38;2 usage : multi-fifo target0 [target1 [target2 [...]]] Where each targetN is a program [...]


Related posts:<ol><li><a href='http://fernandoipar.com/2009/03/09/using-the-enum-data-type-to-increase-performance/' rel='bookmark' title='Permanent Link: Using the ENUM data type to increase performance'>Using the ENUM data type to increase performance</a> <small>While going through the DATA TYPES section of the Certification...</small></li><li><a href='http://fernandoipar.com/2009/08/14/generating-data-with-dbmonster/' rel='bookmark' title='Permanent Link: Generating data with dbmonster'>Generating data with dbmonster</a> <small> In my last post I included some sample data...</small></li><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple shell script to stream data to multiple processes. It has many applications, but the reason I wrote it is to stream the same data to multiple netcat processes on remote machines.</p>
<p>Here&#8217;s the code:</p>
<pre>
#!/bin/bash</code>

usage()
{
cat &lt;&amp;2

usage : multi-fifo target0 [target1 [target2 [...]]]

Where each targetN is a program you want to send the input multi-fifo receives

EOF

}

[ $# -eq 0 ] &amp;&amp; usage &amp;&amp; exit 1

i=0
pipeline="tee /dev/null"
while [ -n "$1" ]; do
target=$1
fifo=/tmp/multi-fifo-$$.$i
mkfifo $fifo
eval "$target &lt; $fifo" &amp;
pipeline="$pipeline | tee $fifo"
i=$((i+1))
shift
done

eval "cat | $pipeline"

j=0
while [ $j -lt $i ]; do
rm -f /tmp/multi-fifo-$$.$j
j=$((j+1))
done
</pre>
<p>Usage is pretty straightforward:</p>
<p><code><br />
tar cjvf - . | ./multi-fifo "nc host1 9999" "nc host2 9999" &gt;/dev/null<br />
</code></p>
<p>This way, the script will create a fifo for each netcat client, then send it&#8217;s stdin to a pipeline that writes to both fifos using tee. Useful for example if you want to stream a backup to multiple destination servers for cloning.</p>


<p>Related posts:<ol><li><a href='http://fernandoipar.com/2009/03/09/using-the-enum-data-type-to-increase-performance/' rel='bookmark' title='Permanent Link: Using the ENUM data type to increase performance'>Using the ENUM data type to increase performance</a> <small>While going through the DATA TYPES section of the Certification...</small></li><li><a href='http://fernandoipar.com/2009/08/14/generating-data-with-dbmonster/' rel='bookmark' title='Permanent Link: Generating data with dbmonster'>Generating data with dbmonster</a> <small> In my last post I included some sample data...</small></li><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running commands from the shell with a timeout (pt 2)</title>
		<link>http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/</link>
		<comments>http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 22:45:33 +0000</pubDate>
		<dc:creator>fernando</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://fernandoipar.com/?p=29</guid>
		<description><![CDATA[Here&#8217;s an improved version of the safecmd script. This one doesn&#8217;t always wait for $timeout seconds even if the command you&#8217;re running exits successfully. In that case, it kills the monitor script and ends at the proper time. Here&#8217;s the code: #!/bin/bash timeout=$1 command=$2 shift 2 check_pid_name() { command=$1 childpid=$2 [ -d /proc/$childpid ] &#124;&#124; [...]


Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout'>Running commands from the shell with a timeout</a> <small>Sometimes, in a shell script, you need to run a...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an improved version of the safecmd script. This one doesn&#8217;t always wait for $timeout seconds even if the command you&#8217;re running exits successfully. In that case, it kills the monitor script and ends at the proper time. </p>
<p>Here&#8217;s the code: </p>
<pre>
#!/bin/bash 

timeout=$1
command=$2
shift 2

check_pid_name()
{
        command=$1
        childpid=$2
        [ -d /proc/$childpid ] || return 1
        [ $(grep -c $command /proc/$childpid/cmdline 2>/dev/null) -gt 0 ] &#038;&#038; return 0 || return 1
}

[ -z "$command" ] &#038;&#038; echo "Usage: safecmd <timeout> <command> [args]">&#038;2 &#038;&#038; exit 1

safe_run()
{
        command=$1
        shift
        $command $*
        kill $$
}

safe_run $command $* &#038;
childpid=$!
sleep $timeout 

check_pid_name $command $childpid &#038;&#038; {
        kill $childpid
        sleep 0.1
        check_pid_name $command $childpid &#038;&#038; kill -9 $childpid
        echo "$command $* timed out"
}
</pre>
<p>You can try it how like this, for example: </p>
<pre>
./safecmd 2 sleep 4
</pre>
<p>which will output</p>
<pre>
./safecmd.1: line 34: 11739 Terminated               safe_run $command $*
sleep 4 timed out
</pre>
<pre>
or ./safecmd 2 sleep 1
</pre>
<p>which will output</p>
<pre>
Terminated
</pre>
<p>Indicating that sleep 1 finished successfully</p>
<p>This is still missing something. You can&#8217;t test for the success of the command you pass safecmd, if it exits successfully you&#8217;ll see the same return code than if it would have exited with an error. </p>
<p>In order to improve this, as far as I can tell, you have to use two scripts, one just as the wrapper (to use one separate script instead of the function safe_run()). That&#8217;s the way this is handled in <a href="http://highbase.seriema-systems.com">Highbase</a>, and I&#8217;ll post a full example in my next post on this subject. </p>


<p>Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout'>Running commands from the shell with a timeout</a> <small>Sometimes, in a shell script, you need to run a...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Running commands from the shell with a timeout</title>
		<link>http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/</link>
		<comments>http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 02:21:49 +0000</pubDate>
		<dc:creator>fernando</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://fernandoipar.com/?p=20</guid>
		<description><![CDATA[Sometimes, in a shell script, you need to run a command that might go on forever (or an amount of time long enough to be called forever) due to poor coding of the software and wrong external conditions (network down or overloaded, overloaded system, a changed private key, etc). In order to avoid this situations, [...]


Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li><li><a href='http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/' rel='bookmark' title='Permanent Link: Piping data to multiple processes'>Piping data to multiple processes</a> <small>Here&#8217;s a simple shell script to stream data to multiple...</small></li></ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Sometimes, in a shell script, you need to run a command that might go on forever (or an amount of time long enough to be called forever) due to poor coding of the software and wrong external conditions (network down or overloaded, overloaded system, a changed private key, etc).</p>
<p>In order to avoid this situations, here&#8217;s a small shell script that will execute any command with a guaranteed timeout:</p>
<pre>#!/bin/bash

timeout=$1
command=$2

check_pid_name()
{
        command=$1
        childpid=$2
        [ -d /proc/$childpid ] || return 1
        [ $(grep -c $command /proc/$childpid/cmdline 2&gt;/dev/null) -gt 0 ] &amp;&amp; return 0 || return 1
}

[ -z "$command" ] &amp;&amp; echo "I need a command to run"&gt;&amp;2 &amp;&amp; exit 1

[ $# -eq 2 ] &amp;&amp; shift || shift 2

$command $* &amp;
childpid=$!

sleep $timeout 

check_pid_name $command $childpid &amp;&amp; {
        kill $childpid
        sleep 0.1
        check_pid_name $command $childpid &amp;&amp; kill -9 $childpid
        echo "$command $* timed out"
}</pre>
<p>As you can see, it&#8217;s quite simple. You just have to start the desired command in the background, sleep for the timeout, and then kill the process if it&#8217;s still running.</p>
<p>Yes, this script has a defect, all the commands will wait $timeout to run, but that&#8217;s easy to solve. I&#8217;ll post an improved (but slightly more complex) version in my next post.</p>


<p>Related posts:<ol><li><a href='http://fernandoipar.com/2009/01/12/running-commands-from-the-shell-with-a-timeout-pt-2/' rel='bookmark' title='Permanent Link: Running commands from the shell with a timeout (pt 2)'>Running commands from the shell with a timeout (pt 2)</a> <small>Here&#8217;s an improved version of the safecmd script. This one...</small></li><li><a href='http://fernandoipar.com/2009/02/04/generating-random-salts-from-bash/' rel='bookmark' title='Permanent Link: Generating random salts from bash'>Generating random salts from bash</a> <small>From the &#8216;just because it can be done&#8217; column, here...</small></li><li><a href='http://fernandoipar.com/2011/03/10/piping-data-to-multiple-processes/' rel='bookmark' title='Permanent Link: Piping data to multiple processes'>Piping data to multiple processes</a> <small>Here&#8217;s a simple shell script to stream data to multiple...</small></li></ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://fernandoipar.com/2009/01/10/running-commands-from-the-shell-with-a-timeout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

