Bash Sub Shells

2010-03-02 148 words 1 min read

Taken from <a href="http://www.linuxjournal.com/content/bash-sub-shells" target="_blank">here.

<span class=\"c\">#!/bin/bash</span>

<span class="nv">server_cmd</span><span class="o">=</span>server <span class="nv">pid_file</span><span class="o">=</span><span class="k">$(</span>basename <span class="nv">$server_cmd</span> .sh<span class="k">)</span>.pid <span class="nv">log_file</span><span class="o">=</span><span class="k">$(</span>basename <span class="nv">$server_cmd</span> .sh<span class="k">)</span>.log

<span class="o">(</span> <span class="nb">echo</span> <span class="s2">"Starting server"</span> <span class="nb">echo</span> <span class="s2">"Doing some init work"</span> <span class="nv">$server_cmd</span> <span class="c"># server becomes a daemon</span>

&lt;span class=\"k\">while &lt;/span>&lt;span class=\"nb\">true&lt;/span>

<span class="nb"> </span><span class="k">do</span> <span class="k"> if</span> <span class="o">[[</span> -f <span class="nv">$pid_file</span> <span class="o">]]</span>; <span class="k">then</span> <span class="k"> </span>sleep 15 <span class="k">else</span> <span class="k"> </span><span class="nb">break</span> <span class="nb"> </span><span class="k">fi</span> <span class="k"> done</span> <span class="k"> </span>mail -s <span class="s2">"Server exitted"</span> joe@blow.com <<<CRAP

<span class="o">)</span> 2>&1 >> <span class="nv">$log_file</span> &

<span class="nb">echo</span> <span class="s2">"Server started"</span>

Now this was something which is not new that you can run multiple commands together with &#8221;()&#8221; or that you can run a command in the background with &#8221;&&#8221;, but putting it together in this fashion is really good. I am impressed.


Tags: bash Linux shell

author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it