<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Code in the hole (Entries tagged as bash)</title>
    <link>http://codeinthehole.com/</link>
    <description>David Winterbottom</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.3.1 - http://www.s9y.org/</generator>
    
    

<item>
    <title>The most important command-line tip - incremental history searching with .inputrc</title>
    <link>http://codeinthehole.com/archives/17-The-most-important-command-line-tip-incremental-history-searching-with-.inputrc.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/17-The-most-important-command-line-tip-incremental-history-searching-with-.inputrc.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=17</wfw:comment>

    <slash:comments>6</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=17</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;Getting &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/www.commandlinefu.com&#039;);&quot;  href=&quot;http://www.commandlinefu.com&quot;&gt;www.commandlinefu.com&lt;/a&gt; off the ground has renewed my interest in Bash, UNIX and all things command-line.  Powerful one-liners are things of beauty and are worth collecting; however what I consider to be the most influential command-line tip I know covers four:&lt;/p&gt;
&lt;code class=&quot;prettyprint&quot;&gt;&quot;\e[A&quot;: history-search-backward
&quot;\e[B&quot;: history-search-forward
&quot;\e[C&quot;: forward-char
&quot;\e[D&quot;: backward-char&lt;/code&gt;
&lt;p&gt;These lines need to be placed in your &lt;code&gt;~/.inputrc&lt;/code&gt; file, the start-up script for the Readline utility used by Bash (as well as several other applications) and others).   The important commands here are the first two, which bind your up and down cursor keys to incrementally search your history. (The second two ensure that left and right continue to work correctly).&lt;/p&gt;
&lt;p&gt;This is &lt;em&gt;incredibly useful&lt;/em&gt; for retrieving commands you&#039;ve used previously and makes a huge difference to your productivity.  For instance, to find a previous SSH command from a few days ago, simply type &quot;ss&quot; and press up a few times.  This will allow you to browse through all your previous ssh&amp;hellip; commands until you find the right one - you never need to use more than 4 or 5 keystrokes to retrieve any previous command.  If your cycling through too many commands to find the right one, type in a few more characters to refine the search.&lt;/p&gt;
&lt;p&gt;As indicated above, this functionality is available in all applications that use Readline including MySQL, Python, IRB (interactive Ruby shell) and others.  Once you&#039;re used to this feature, it&#039;s hard to live without - the first thing I do once I&#039;ve been set up as a user on a new server is update my .inputrc file to contain these settings.  The one place where I sorely wish this functionality existed is the Firebug Javascript commandline in Firefox (a ticket already exists requesting a similar feature).&lt;/p&gt;
&lt;p&gt;Another way of searching your history is to use CTRL+R, which essentially performs a full-text search on your history (keep pressing CTRL+R to cycle through results).  In this case, searching for &quot;ssh&quot; will locate all commands that feature this string anywhere in the command.  Although this is actually a more powerful feature than the incremental history search described above, I don&#039;t often use it as: (a) the incremental search generally lets me jump to the desired command in fewer key-presses and (b) I find &quot;ssh&quot;, CTRL+R, CTRL+R slightly awkward to type and less intuitive than &quot;ssh&quot;, UP, UP.  Horses for courses really - you could probably be just as efficient with either one.
&lt;p&gt;&amp;raquo; Hat-tip to the place where I first learnt this: &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/www.ukuug.org/events/linux2003/papers/bash_tips/&#039;);&quot;  href=&quot;http://www.ukuug.org/events/linux2003/papers/bash_tips/&quot;&gt;Power Shell Usage by Simon Myers&lt;/a&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;One extra thing: this functionality can be neatly complimented by some choice history settings in your &lt;code&gt;~/.bashrc&lt;/code&gt; file:&lt;/p&gt;
&lt;code class=&quot;prettyprint&quot;&gt;export HISTSIZE=1000000
export HISTFILESIZE=1000000000&lt;/code&gt;
&lt;p&gt;These simply set your history to be very large so that you have a huge bank of commands to search.&lt;/p&gt;
 
    </content:encoded>

    <pubDate>Tue, 03 Feb 2009 23:40:00 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/17-guid.html</guid>
    <category>bash</category>
<category>commandlinefu</category>

</item>
<item>
    <title>Monitoring MySQL with Ganglia and gmetric</title>
    <link>http://codeinthehole.com/archives/8-Monitoring-MySQL-with-Ganglia-and-gmetric.html</link>
            <category>Monitoring</category>
    
    <comments>http://codeinthehole.com/archives/8-Monitoring-MySQL-with-Ganglia-and-gmetric.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=8</wfw:comment>

    <slash:comments>4</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=8</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;div class=&quot;book_cover&quot;&gt;
&lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/www.amazon.co.uk/gp/product/0596102356?ie=UTF8&amp;amp;tag=codinthehol-21&amp;amp;linkCode=as2&amp;amp;camp=1634&amp;amp;creative=6738&amp;amp;creativeASIN=0596102356&#039;);&quot;  href=&quot;http://www.amazon.co.uk/gp/product/0596102356?ie=UTF8&amp;tag=codinthehol-21&amp;linkCode=as2&amp;camp=1634&amp;creative=6738&amp;creativeASIN=0596102356&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;http://codeinthehole.com/app/images/books/building-scalable-websites.jpg&quot;&gt;&lt;/a&gt;&lt;img src=&quot;http://www.assoc-amazon.co.uk/e/ir?t=codinthehol-21&amp;l=as2&amp;o=2&amp;a=0596102356&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Following some server monitoring advice from the excellent &#039;Building Scalable Web Sites&#039; by Cal Henderson, I&#039;ve recently been experimenting with &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/ganglia.info/&#039;);&quot;  href=&quot;http://ganglia.info/&quot; title=&quot;Ganglia homepage&quot;&gt;Ganglia&lt;/a&gt; on a cluster of servers at &lt;a href=&quot;www.tangentlabs.co.uk&quot; title=&quot;Tangent Labs&quot;&gt;Tangent Labs&lt;/a&gt;.  It has proved to be deeply impressive and has given us a great tool for keeping an eye on how our servers are performing, as well as providing an invaluable diagnostic tool should things go wrong.&lt;/p&gt;

&lt;p&gt;In essence, Ganglia is a distributed monitoring application that allows statistics on a cluster of servers to be aggregated in a single place.  Moreover, an excellent web front-end is also provided to view charts of the statistics being collected.  You can view a &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/monitor.millennium.berkeley.edu/&#039;);&quot;  href=&quot;http://monitor.millennium.berkeley.edu/&quot; title=&quot;Live demo of Ganglia&quot;&gt;live demo of the Ganglia monitoring application for the UC Berkeley Millennium Project&lt;/a&gt; which shows exactly what Ganglia can provide.&lt;/p&gt;

&lt;p&gt;Out of the box, Ganglia collects basic server stats on load, memory, CPU, network and disk.  However, what makes it great is that it can be easily extended to collect statistics on anything that you can measure from the command line.  There is a &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/ganglia.sourceforge.net/gmetric/&#039;);&quot;  href=&quot;http://ganglia.sourceforge.net/gmetric/&quot; title=&quot;Visit the repository&quot;&gt;repository of user-contributed scripts&lt;/a&gt; although it&#039;s not that extensive.  In this post, I detail a script that can used to feed statistics about MySQL into Ganglia for monitoring - something important to any LAMP-stack application.&lt;/p&gt;&lt;p&gt;A few technical details first: Ganglia utilises three utilities to run each with a distinct responsibility:&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;gmond&lt;/dt&gt;&lt;dd&gt;A daemon responsible for collecting monitoring stats and sharing them using XML over TCP.   The gmond daemon runs on all nodes that you want to monitor.&lt;/dd&gt;
&lt;dt&gt;gmetad&lt;/dt&gt;&lt;dd&gt;A daemon responsible for collecting and storing data from other gmetad and gmomd services.  This only needs to be running on a single, central node.&lt;/dd&gt;
&lt;dt&gt;gmetric&lt;/dt&gt;&lt;dd&gt;A command-line application used for using Ganglia to monitor custom metrics.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;All very simple really - see &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/ganglia.wiki.sourceforge.net/ganglia_documents&#039;);&quot;  href=&quot;http://ganglia.wiki.sourceforge.net/ganglia_documents&quot; title=&quot;Ganglia docs&quot;&gt;the docs for the nitty gritty&lt;/a&gt;.  Ok, enough intro - here&#039;s my script for MySQL:&lt;/p&gt;

&lt;p&gt;&amp;raquo; &lt;a href=&quot;http://codeinthehole.com/app/downloads/gmetric-mysql.sh&quot;&gt;gmetric-mysql.sh (3.1kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It&#039;s written in Bash and uses a temporary file to read the MySQL stats into before parsing and submitting to gmetric.  This is done as many of the MySQL stats of interest are cumulative counts rather than rates (eg SELECTs per second) which are generally of more interest.  Hence, the script estimates the rate of change by dividing the value delta by the timestamp delta.&lt;/p&gt;

&lt;p&gt;To use this script, simply copy it into a convenient location, ensure it is executable and add the following line to your crontab.&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;* * * * * /path/to/gmetric-mysql.sh &amp;gt; /dev/null &lt;span style=&quot;color: #cc66cc;&quot;&gt;2&lt;/span&gt;&amp;gt;&amp;amp;&lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;As soon as this is done, your Ganglia web front-end should start plotting the new metrics.&lt;/p&gt;
&lt;img src=&quot;http://codeinthehole.com/app/images/ganglia-snapshot.jpeg&quot; alt=&quot;Sample Ganglia charts&quot; /&gt;

 
    </content:encoded>

    <pubDate>Sun, 14 Dec 2008 21:34:00 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/8-guid.html</guid>
    <category>bash</category>
<category>ganglia</category>
<category>linux</category>
<category>monitoring</category>
<category>mysql</category>

</item>
<item>
    <title>Checking all MySQL tables</title>
    <link>http://codeinthehole.com/archives/10-Checking-all-MySQL-tables.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/10-Checking-all-MySQL-tables.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=10</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=10</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;It&#039;s well known that MyISAM tables are prone to corruption and need to be regularly checked and repaired.  Moreover, in a production environment, it can be beneficial to run a daily check of all tables and mail news of any errors to an appropriate developer/DBA.&lt;/p&gt;
&lt;p&gt;There are two options for checking MySQL tables.  The most effective method is to run the &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/doc/refman/5.0/en/myisamchk.html&#039;);&quot;  href=&quot;http://dev.mysql.com/doc/refman/5.0/en/myisamchk.html&quot; title=&quot;Manual page for myisamchk&quot;&gt;myisamchk utility&lt;/a&gt; directly on the index files (.MYI) of the tables in question (some simple shell expansion makes this easy):&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;myisamchk --silent --fast /path/to/datadir/*/*.MYI&lt;/div&gt;
&lt;p&gt;However, this proses a problem in that you must ensure that no other programs are accessing the tables while they are being checked.  Hence they must be locked, or better still, the MySQL daemon stopped before running any checks.  Perversely, if this is not done, the act of checking the tables can corrupt them.&lt;/p&gt;
&lt;p&gt;Another option is to use the CHECK TABLE syntax in SQL (which does not pose a risk of corruption).  There are various scripts (written in PHP and bash) posted on the &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/dev.mysql.com/doc/mysql/en/CHECK_TABLE.html&#039;);&quot;  href=&quot;http://dev.mysql.com/doc/mysql/en/CHECK_TABLE.html&quot; title=&quot;The manual page for CHECK TABLE&quot;&gt;CHECK TABLE manual page&lt;/a&gt; but this operation can be done easily through a single line:
&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt; -B -e &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;SHOW TABLES&quot;&lt;/span&gt; \&lt;br /&gt;| awk &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;{print &amp;quot;CHECK TABLE &amp;quot;$1&amp;quot;;&amp;quot;}&#039;&lt;/span&gt; \&lt;br /&gt;| mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt;&lt;/div&gt;
&lt;p&gt;This dynamically creates a list of &quot;CHECK TABLE ...&quot; commands which is piped into MySQL for execution. 

&lt;p&gt;For checking a selection of tables rather than all, use the LIKE operator when selecting the tables to check:&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt; -B -e &lt;span style=&quot;color: #ff0000;&quot;&gt;&quot;SHOW TABLES LIKE &#039;User%&#039;&quot;&lt;/span&gt; \&lt;br /&gt;| awk &lt;span style=&quot;color: #ff0000;&quot;&gt;&#039;NR != 1 {print &amp;quot;CHECK TABLE &amp;quot;$1&amp;quot;;&amp;quot;}&#039;&lt;/span&gt; \&lt;br /&gt;| mysql -p&amp;lt;password&amp;gt; -D&amp;lt;database&amp;gt;&lt;/div&gt;
&lt;p&gt;This only checks the tables that start &#039;User&#039;.  Note that the awk program has an extra clause to ensure that the first line of MySQL output is skipped.&lt;/p&gt; 
    </content:encoded>

    <pubDate>Sun, 23 Nov 2008 11:30:40 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/10-guid.html</guid>
    <category>bash</category>
<category>mysql</category>

</item>
<item>
    <title>Monitoring MySQL processes</title>
    <link>http://codeinthehole.com/archives/2-Monitoring-MySQL-processes.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/2-Monitoring-MySQL-processes.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=2</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=2</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;Just a quick tip on monitoring the queries that mysql is handling on a production site.  You can use the mysqladmin tool to return a list of the processes currently being handled. Combining this with the UNIX watch command allows a real-time monitoring of what&#039;s going on. &lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;watch -n &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt; mysqladmin processlist&lt;/div&gt;
&lt;p&gt;The &quot;-n 1&quot; specifies that mysqladmin executes every second.  Depending on your set-up, you may need to specify a mysql user and password:&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;watch -n &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt; mysqladmin --&lt;span style=&quot;color: #0000ff;&quot;&gt;user=&lt;/span&gt;&amp;lt;user&amp;gt; --&lt;span style=&quot;color: #0000ff;&quot;&gt;password=&lt;/span&gt;&amp;lt;password&amp;gt; processlist&lt;/div&gt; 
    </content:encoded>

    <pubDate>Sun, 26 Oct 2008 22:06:00 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/2-guid.html</guid>
    <category>bash</category>
<category>linux</category>
<category>mysql</category>

</item>
<item>
    <title>Following log files with tail -f</title>
    <link>http://codeinthehole.com/archives/1-Following-log-files-with-tail-f.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/1-Following-log-files-with-tail-f.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=1</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=1</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;UNIX is a majestic onion of discovery.  Every day a new layer of understanding can be peeled away to give some new pungent goodness. Today&#039;s was the &#039;follow&#039;
option of the tail command. &lt;/p&gt;
&lt;p&gt;
It&#039;s commonplace to use tail for viewing the recent entries to a log file:&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;tail /var/log/apache2/error.log&lt;/div&gt;
&lt;p&gt;Much more useful is set the &#039;follow&#039; option so that, rather than echoing to STDOUT and returning control to the prompt -  tail continues to watch the file in question and echos additional lines to the terminal.  This can be very useful during development - I often leave a terminal open watching the error logs while I develop - so that I can quickly pounce on any errors that pop up.&lt;/p&gt;
&lt;div class=&quot;bash&quot; style=&quot;text-align: left&quot;&gt;tail -f my-app-error.log&lt;/div&gt;
&lt;p&gt;
Return control to the command prompt using CTRL+C (or run as a background process using &amp;).  This can be quite useful for monitoring the error logs of production sites where echoing error messages and warnings to the screen would be unacceptable.
&lt;/p&gt; 
    </content:encoded>

    <pubDate>Wed, 22 Oct 2008 09:03:00 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/1-guid.html</guid>
    <category>bash</category>
<category>linux</category>

</item>

</channel>
</rss>