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>
<span class=\"k\">while </span><span class=\"nb\">true</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 ”()” or that you can run a command in the background with ”&”, but putting it together in this fashion is really good. I am impressed.
Related Articles:
- 2010/01/21 sed tutorial and help
- 2009/11/05 Linux xdg-open — replacement for start in windows
- 2009/07/20 bash tutorial for begineer and experienced.
- 2010/02/10 Annotating the photograph with comments.
- 2010/02/10 Script to Watch for Ubuntu 9.10 Launch – can be used in other scenarios.
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.