<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Linux by Examples</title>
	
	<link>http://linux.byexamples.com</link>
	<description>We explain every GNU/Linux command by examples in this blog!</description>
	<pubDate>Thu, 28 May 2009 16:02:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/LinuxByExamples" type="application/rss+xml" /><feedburner:emailServiceId>LinuxByExamples</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Does scons outperforms autoconf and makefiles?</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/b67g0V5Q3vU/</link>
		<comments>http://linux.byexamples.com/archives/508/does-scons-outperforms-autoconf-and-makefiles/#comments</comments>
		<pubDate>Thu, 28 May 2009 15:59:26 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=508</guid>
		<description><![CDATA[SCons is a software construction tool (build tool, or make tool) implemented in Python, that uses Python scripts as &#8220;configuration files&#8221; for software builds. Based on the design that won the Software Carpentry build tool competition, SCons solves a number of problems associated with other build tools, especially including the classic and ubiquitous Make itself.
In [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>SCons is a software construction tool (build tool, or make tool) implemented in Python, that uses Python scripts as &#8220;configuration files&#8221; for software builds. Based on the design that won the Software Carpentry build tool competition, SCons solves a number of problems associated with other build tools, especially including the classic and ubiquitous Make itself.</p>
<p>In its evolution, SCons will be more general than a make replacement, it will be a Gnu Build System replacement. The Gnu Build System (GBS) is also known as the set of Autotools (autoconf, automake, autoheader, etc&#8230;).</p>
<p>Quote From <a href="http://www.scons.org/wiki/TheBigPicture">http://www.scons.org/wiki/TheBigPicture</a></p></blockquote>
<p>So scons is just a makefile replacement? Which claims to be simple and feature rich? Let me construct a simple but sufficient scons configuration file (SConstruct), and show you how you can compile your c++ projects. Then you make the judgement whether to go for it or not.</p>
<pre><code>
#filename: SConstruct
env = Environment(
CXX = 'g++34',
CCFLAGS = ['-g','-O3','-Wall','-DPOSIX_C_SOURCE=199506L','-D_REENTRANT','-DLINT_ARGS','-DLINUXOS'],
LIBS = ['pthread', 'c','nsl','rt' ,'lbestd'],
LIBPATH = ['lib'],
CPPPATH = ['.','lib/inc'],
LINKFLAGS = ['-mt']
)

env.Program(
'bin/msgQ',
['Main.cc', 'MainState.cc','MainFunc.cc',
'MyLog.cc','MyQueue.cc','MyTcp.cc',
'TcpSvr.cc']
)
</code></pre>
<p>To compile the source codes, simply do this:</p>
<pre><code>scons</code></pre>
<p>To clear the objects and binaries, simply do this:</p>
<pre><code>scons -c</code></pre>
<p>Let me briefly explain the items in the SConstruct file.</p>
<p>By default, scons does define a simple compilation environment for you, if you write helloworld.cc, you just need to write a line in SConstruct.</p>
<pre><code>Program('helloworld.cc')</code></pre>
<p>But the case above, I overwrite some environment variable&#8217;s value for my needs to build a complex application.</p>
<p>CXX is the variable defines you c++ compiler. My case, I change it to g++34.</p>
<p>You can check the default value by just insert a python print statement in your SConstruct file.</p>
<pre><code>env = Environment()
print "CXX = ", env['CXX']</code></pre>
<p>CCFLAGS defines compilation flags.<br />
LIBS defines library to link.<br />
LIBPATH defines additional library paths besides the system default.<br />
CPPPATH defines include file paths.<br />
LINKFLAGS defines compiler linker flags</p>
<p>After constructed the environment, just compile your binary by calling <strong>Program</strong> function.</p>
<p>scons have more features compiles java codes too and importantly it is cross platform. To know more? Check out the scons <a href="http://www.scons.org/doc/production/HTML/scons-man.html">man pages</a> ans well as <a href="http://www.scons.org/doc/production/HTML/scons-user.html">user manual</a>.</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/156/installing-from-tarballs/" rel="bookmark" title="Permanent Link: Installing From Tarballs" >Installing From Tarballs</a></span><div class="aizatto_related_posts_excerpt">We usually download linux programs through package handling tools such as yum and apt-get. Download programs through pac...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/345/pcregrep-grep-based-on-perl-compatible-regular-expressions/" rel="bookmark" title="Permanent Link: pcregrep, grep based on perl compatible regular expressions" >pcregrep, grep based on perl compatible regular expressions</a></span><div class="aizatto_related_posts_excerpt">grep supposedly support perl regex by specified -P option, but somehow my grep do not support that, it complains this wh...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/328/compile-fluxbox-under-ubuntu/" rel="bookmark" title="Permanent Link: Compile Fluxbox under Ubuntu" >Compile Fluxbox under Ubuntu</a></span><div class="aizatto_related_posts_excerpt">I make a fresh install of kubuntu and apt-get Fluxbox from Ubuntu repository yesterday and tried to login to Fluxbox. Fi...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/Mk4CtY3gwQaib68sxwFDqL5wKhM/0/da"><img src="http://feedads.g.doubleclick.net/~a/Mk4CtY3gwQaib68sxwFDqL5wKhM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/Mk4CtY3gwQaib68sxwFDqL5wKhM/1/da"><img src="http://feedads.g.doubleclick.net/~a/Mk4CtY3gwQaib68sxwFDqL5wKhM/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/b67g0V5Q3vU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/508/does-scons-outperforms-autoconf-and-makefiles/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/508/does-scons-outperforms-autoconf-and-makefiles/</feedburner:origLink></item>
		<item>
		<title>python: simple http server for file sharing</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/N_57IDAvqX4/</link>
		<comments>http://linux.byexamples.com/archives/506/python-simple-http-server-for-file-sharing/#comments</comments>
		<pubDate>Fri, 15 May 2009 00:39:40 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=506</guid>
		<description><![CDATA[You do not need to setup apache server just to hosting your file for sharing. If you have python 2.5 and above, you can do this at the targeted directory to share.
python -m SimpleHTTPServer 9914
9914 is the port number I choose to host my web, assume my IP is 192.168.1.1, in firefox you can ask [...]]]></description>
			<content:encoded><![CDATA[<p>You do not need to setup apache server just to hosting your file for sharing. If you have python 2.5 and above, you can do this at the targeted directory to share.</p>
<pre><code>python -m SimpleHTTPServer 9914</code></pre>
<p>9914 is the port number I choose to host my web, assume my IP is 192.168.1.1, in firefox you can ask your friend to access this url: <a href="http://192.168.1.1:9914">http://192.168.1.1:9914</a>.</p>
<p>It is easy isn&#8217;t it? You do NOT even need to run it as root, just run it as normal user but make sure your firewall allow the chosen port number.</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/343/python-handle-string-from-pipelines-and-list-of-param/" rel="bookmark" title="Permanent Link: python: handle string from pipelines and list of param" >python: handle string from pipelines and list of param</a></span><div class="aizatto_related_posts_excerpt">While I was searching ways to implement pipeline input for my python apps, I read an article from linuxjournal.com that ...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/494/how-can-i-avoid-running-a-python-script-multiple-times-implement-file-locking/" rel="bookmark" title="Permanent Link: How can I avoid running a python script multiple times? Implement file locking." >How can I avoid running a python script multiple times? Implement file locking.</a></span><div class="aizatto_related_posts_excerpt">Sometimes we just need a single instance of the python script to run at a time. Meaning, the python script itself should...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/77/redirect-stderr-to-stdout/" rel="bookmark" title="Permanent Link: Redirect stderr to stdout" >Redirect stderr to stdout</a></span><div class="aizatto_related_posts_excerpt">Very simple, just remember:

	1 = stdout
	2 = stderr

In python, its unittest module loves to dump test results to ...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/pkUPPrAgG6v62PCIDflkGndUJIo/0/da"><img src="http://feedads.g.doubleclick.net/~a/pkUPPrAgG6v62PCIDflkGndUJIo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/pkUPPrAgG6v62PCIDflkGndUJIo/1/da"><img src="http://feedads.g.doubleclick.net/~a/pkUPPrAgG6v62PCIDflkGndUJIo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/N_57IDAvqX4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/506/python-simple-http-server-for-file-sharing/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/506/python-simple-http-server-for-file-sharing/</feedburner:origLink></item>
		<item>
		<title>sometimes, grep alone is enough to do the task</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/_dXZTaK2-X0/</link>
		<comments>http://linux.byexamples.com/archives/503/sometimes-grep-alone-is-enough-to-do-the-task/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 10:47:39 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Text Manipulation]]></category>

		<category><![CDATA[grep]]></category>

		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=503</guid>
		<description><![CDATA[One thing that makes UNIXes command line so powerful is because of pipelines. Pipelines is a set of processes chained by their standard streams, so that the output of each process (stdout) feeds directly as input (stdin) of the next one. Simple words, pipelines pass the outcomes of a particular command as input to another [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that makes UNIXes command line so powerful is because of pipelines. Pipelines is a set of processes chained by their standard streams, so that the output of each process (stdout) feeds directly as input (stdin) of the next one. Simple words, pipelines pass the outcomes of a particular command as input to another command, and these chained allows you to do a command line combo!</p>
<p>A complex combo, I have constructed one last time in post <a href="http://linux.byexamples.com/archives/332/what-is-your-10-common-linux-commands/">what is your 10 common linux commands?</a></p>
<pre><code>
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' |  \
grep -v "./" | \
column -c3 -s " " -t | \
sort -nr | nl |  head -n10
</pre>
<p></code></p>
<p>But sometimes we tend to misuse them, I found someone likes to do this:</p>
<pre><code>cat foobar.txt | grep "barfoo"</code></pre>
<p>Actually grep alone is enough to do the search, we do not need cat or meow for that. As I show him this:</p>
<pre><code>grep "barfoo" foobar.txt</code></pre>
<p>Another example is list the names of all files under a given directory that match a particular string. Let say I wanna obtain a list of files that have string &#8220;foobar&#8221;.</p>
<pre><code>find /opt/src/kmess -type f | xargs grep -l "foobar"</code></pre>
<p>Hey! find is cool but grep alone is enough to do the job.</p>
<pre><code>grep -lr "foobar" /opt/src/kmess</code></pre>
<p>Try to goes through the grep manual, you will find out that grep is more powerful than what you expect.</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/257/sort-grep-c-results/" rel="bookmark" title="Permanent Link: Sort grep -c results" >Sort grep -c results</a></span><div class="aizatto_related_posts_excerpt">grep -c "TODO:" *.c

foo.c:1
bar.c:0
spam.c:44
egg.c:32
matrix.c:0
grep -c displays the total count of matching l...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/142/matching-whole-word-in-grep/" rel="bookmark" title="Permanent Link: Matching Whole Word in Grep" >Matching Whole Word in Grep</a></span><div class="aizatto_related_posts_excerpt">Is "grep" capable to match a whole word? The answer of cause is yes!!
grep -w "keyword" filename
this will only displa...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/140/exclusive-grep/" rel="bookmark" title="Permanent Link: Exclusive Grep" >Exclusive Grep</a></span><div class="aizatto_related_posts_excerpt">When you have 'grep' some keyword:
grep "keyword" filename
tons of lines and dozen of  words you don't want may show o...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/jFA7YKPvR5JBE7XBV7tPxfzR27U/0/da"><img src="http://feedads.g.doubleclick.net/~a/jFA7YKPvR5JBE7XBV7tPxfzR27U/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/jFA7YKPvR5JBE7XBV7tPxfzR27U/1/da"><img src="http://feedads.g.doubleclick.net/~a/jFA7YKPvR5JBE7XBV7tPxfzR27U/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/_dXZTaK2-X0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/503/sometimes-grep-alone-is-enough-to-do-the-task/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/503/sometimes-grep-alone-is-enough-to-do-the-task/</feedburner:origLink></item>
		<item>
		<title>Bit shifting can be done in python just like in c</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/t-1ZP-r38GI/</link>
		<comments>http://linux.byexamples.com/archives/496/bit-shifting-can-be-done-in-python-just-like-in-c/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 08:31:36 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Developer]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=496</guid>
		<description><![CDATA[It was amazing to discover that I can do bit shifting in python just like in c, the syntax makes no different at all. Let us look at how easy I can do a bit shifting. I write an example loop by shifting the bit leftwards.

for i in range(0,10):
    print "0x0001 &#60;&#60; [...]]]></description>
			<content:encoded><![CDATA[<p>It was amazing to discover that I can do bit shifting in python just like in c, the syntax makes no different at all. Let us look at how easy I can do a bit shifting. I write an example loop by shifting the bit leftwards.</p>
<pre><code>
for i in range(0,10):
    print "0x0001 &lt;&lt; %d = 0x%04X" % ( i , 0x1 &lt;&lt; i)
</code></pre>
<p>The result is</p>
<pre><code>
0x0001 &lt;&lt; 0 = 0x0001
0x0001 &lt;&lt; 1 = 0x0002
0x0001 &lt;&lt; 2 = 0x0004
0x0001 &lt;&lt; 3 = 0x0008
0x0001 &lt;&lt; 4 = 0x0010
0x0001 &lt;&lt; 5 = 0x0020
0x0001 &lt;&lt; 6 = 0x0040
0x0001 &lt;&lt; 7 = 0x0080
0x0001 &lt;&lt; 8 = 0x0100
0x0001 &lt;&lt; 9 = 0x0200
</code></pre>
<p>The important part in the example is this :</p>
<pre><code>0x1 &lt;&lt; i</code></pre>
<p>Let say I wanna shift the bits of a value rightwards 3 times, I will do this:</p>
<pre><code>value = 102
print value &gt;&gt; 3</code></pre>
<p>The result will be 12. </p>
<p>To know more about what is bit shifting, check out <a href="http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts">wikipedia</a>.</p>
<p>Have Fun!</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/365/python-convey-the-exception-traceback-into-log-file/" rel="bookmark" title="Permanent Link: python: convey the exception traceback into log file" >python: convey the exception traceback into log file</a></span><div class="aizatto_related_posts_excerpt">Python is the interpreter language, you do not need to compile your code, and also you have no ways to check for your sy...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/410/python-vs-lua-data-structure/" rel="bookmark" title="Permanent Link: Python vs Lua, data structure" >Python vs Lua, data structure</a></span><div class="aizatto_related_posts_excerpt">In python, we have various type of data structure, such as list, set, tuple, dictionary etc, but in Lua, we only have ta...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/343/python-handle-string-from-pipelines-and-list-of-param/" rel="bookmark" title="Permanent Link: python: handle string from pipelines and list of param" >python: handle string from pipelines and list of param</a></span><div class="aizatto_related_posts_excerpt">While I was searching ways to implement pipeline input for my python apps, I read an article from linuxjournal.com that ...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/1iBMrHNdQa2wXwFnMVRLcezmzw8/0/da"><img src="http://feedads.g.doubleclick.net/~a/1iBMrHNdQa2wXwFnMVRLcezmzw8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1iBMrHNdQa2wXwFnMVRLcezmzw8/1/da"><img src="http://feedads.g.doubleclick.net/~a/1iBMrHNdQa2wXwFnMVRLcezmzw8/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/t-1ZP-r38GI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/496/bit-shifting-can-be-done-in-python-just-like-in-c/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/496/bit-shifting-can-be-done-in-python-just-like-in-c/</feedburner:origLink></item>
		<item>
		<title>How can I avoid running a python script multiple times? Implement file locking.</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/SYPKk8Rri3c/</link>
		<comments>http://linux.byexamples.com/archives/494/how-can-i-avoid-running-a-python-script-multiple-times-implement-file-locking/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 16:07:07 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Developer]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=494</guid>
		<description><![CDATA[Sometimes we just need a single instance of the python script to run at a time. Meaning, the python script itself should detects whether any instances of himself are still running and act accordingly.
Well, how to do it?
The idea is simple, the first instance of the python script will open a file, and lock it. [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we just need a single instance of the python script to run at a time. Meaning, the python script itself should detects whether any instances of himself are still running and act accordingly.</p>
<p>Well, how to do it?</p>
<p>The idea is simple, the first instance of the python script will open a file, and lock it. Therefore when the consequent instances try to lock the file, it will fails.</p>
<p>The code is simple too.</p>
<pre><code>
import fcntl
def lockFile(lockfile):
    fp = open(lockfile, 'w')
    try:
        fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
    except IOError:
        return False

    return True
</code></pre>
<p>The function will try to lock the file specified by me, if it success, return True, else return False.</p>
<p>From any point of my codes, I&#8217;ll do</p>
<pre><code>
if not lockFile(".lock.pod"):
        sys.exit(0)
</code></pre>
<p>Interesting?</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/95/writting-executable-script/" rel="bookmark" title="Permanent Link: writting executable script" >writting executable script</a></span><div class="aizatto_related_posts_excerpt">Shell script (sh), Bash script (bash), Python and perl are all scripting language. By default, to run a scripting file, ...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/343/python-handle-string-from-pipelines-and-list-of-param/" rel="bookmark" title="Permanent Link: python: handle string from pipelines and list of param" >python: handle string from pipelines and list of param</a></span><div class="aizatto_related_posts_excerpt">While I was searching ways to implement pipeline input for my python apps, I read an article from linuxjournal.com that ...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/372/python-threading-callback-timer/" rel="bookmark" title="Permanent Link: Python: Threading Callback Timer" >Python: Threading Callback Timer</a></span><div class="aizatto_related_posts_excerpt">It is very common that we need to run certain routines periodically, thats why in *nix environment we have cron and in w...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/kUW-Lef51OKknk545Kie-0NdHKM/0/da"><img src="http://feedads.g.doubleclick.net/~a/kUW-Lef51OKknk545Kie-0NdHKM/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/kUW-Lef51OKknk545Kie-0NdHKM/1/da"><img src="http://feedads.g.doubleclick.net/~a/kUW-Lef51OKknk545Kie-0NdHKM/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/SYPKk8Rri3c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/494/how-can-i-avoid-running-a-python-script-multiple-times-implement-file-locking/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/494/how-can-i-avoid-running-a-python-script-multiple-times-implement-file-locking/</feedburner:origLink></item>
		<item>
		<title>Plot your graphs with command line gnuplot</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/dYHjk8gmXlM/</link>
		<comments>http://linux.byexamples.com/archives/487/plot-your-graphs-with-command-line-gnuplot/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 09:27:07 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Developer]]></category>

		<category><![CDATA[gnuplot]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=487</guid>
		<description><![CDATA[gnuplot is a command line driven graph plotter tools for us to generate graphs. The common graphs that we are looking forward to present the resource performance per seconds, hours, days, weeks or months are usually plot graphs, which it consist of lines and dots. gnuplot allows us to read the data from text files [...]]]></description>
			<content:encoded><![CDATA[<p>gnuplot is a command line driven graph plotter tools for us to generate graphs. The common graphs that we are looking forward to present the resource performance per seconds, hours, days, weeks or months are usually plot graphs, which it consist of lines and dots. gnuplot allows us to read the data from text files which contains values in tabular format. </p>
<p>For example, I have a module that extract raw values into logs based on the specified interval. The result will looks like this (test.dat):</p>
<pre><code>
##date time slot_1  slot_2  slot_3  slot_4  slot_5  slot_6  slot_7  slot_8  slot_9  slot_10
06/02/2009 09:16:49 16  6  15  5  14  4  17  3  14  4
06/02/2009 09:16:54 16  8  13  3  15  4  17  4  15  5
06/02/2009 09:16:59 18  8  11  5  15  6  14  3  16  5
06/02/2009 09:17:04 19  6  12  5  18  7  15  5  18  5
06/02/2009 09:17:09 19  7  14  6  16  6  16  6  18  4
06/02/2009 09:17:14 21  7  17  5  19  7  16  4  20  5
06/02/2009 09:17:19 20  5  19  6  17  5  15  3  17  3
06/02/2009 09:17:24 20  6  21  5  18  4  14  4  18  4
06/02/2009 09:17:29 17  5  19  4  16  4  15  4  17  2
</code></pre>
<p>gnuplot allows us to create a template describe how our graph outlook. To generate the graph, we just have to execute the template, just like executing bash script.</p>
<p>This is a sample of a gnuplot template that I use to construct a plot graph, (test1.pg)</p>
<pre><code>
#!/usr/bin/gnuplot
reset
set terminal png

set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"
set xlabel "time"

set ylabel "total actives"
set yrange [0:31]

set title "M7YC Performance per time"
set key reverse Left outside
set grid

set style data linespoints

plot "test.dat" using 1:3 title "slot 1", \
"" using 1:4 title "slot 2", \
"" using 1:5 title "slot 3", \
"" using 1:6 title "slot 4", \
"" using 1:7 title "slot 5", \
"" using 1:8 title "slot 6", \
"" using 1:9 title "slot 7", \
"" using 1:10 title "slot 8", \
"" using 1:11 title "slot 9", \
"" using 1:12 title "slot 10"
#</code></pre>
<p>To allow the template to be executable, the first line of the template must add the SHABANG &#8216;#!/usr/bin/gnuplot&#8217;, similar to bash script. And also remember to perform chmod to your template.</p>
<pre><code>chmod +x test1.pg</code></pre>
<p>I would like to generate graph that shows the performance value over time, therefore I have to read datetime into my X axis and performance value into my Y axis. First two columns of my data file are datetime, I need to set the xdata as time, the define the time format in the raw data files and at last define the x format to appear in my graph.</p>
<pre><code>
set xdata time
set timefmt "%d/%m/%Y %H:%M:%S"
set format x "%H:%M"</code></pre>
<p>I would like to display my keys outside the graphs as well as enable the grid to ease the reading.</p>
<pre><code>
set key reverse Left outside
set grid</code></pre>
<p>The plot graph I intended to generate consist of lines and dots.</p>
<pre><code>set style data linespoints</code></pre>
<p>The important part is how to define the source of my raw data, how many items I wanted to plot into the graph, as well as giving each item a title. The plot format may vary based on different graph format, but for my case it is</p>
<pre><code>
plot [raw_data] using [x value's column in data file]:[y value's column in data files] title [item's name], ...
</code></pre>
<p>The plot portion can be shorten into this:</p>
<pre><code>
plot "test.dat" u 1:3 t "slot 1", \
"" u 1:4 t "slot 2", \
"" u 1:5 t "slot 3", \
"" u 1:6 t "slot 4", \
"" u 1:7 t "slot 5", \
"" u 1:8 t "slot 6", \
"" u 1:9 t "slot 7", \
"" u 1:10 t "slot 8", \
"" u 1:11 t "slot 9", \
"" u 1:12 t "slot 10"
</code></pre>
<p>Execute the template and redirect the binary stream to construct a png file.</p>
<pre><code>./test1.pg &gt; test1.png</code></pre>
<p><strong>test1.png</strong><br />
<a href="http://linux.byexamples.com/wp-content/uploads/2009/02/test1.png"><img src="http://linux.byexamples.com/wp-content/uploads/2009/02/test1.png" alt="" title="test1"/></a></p>
<p>gnuplot is far more powerful than what I have illustrate here, you may want to checkout the <a href="http://www.gnuplot.info/">official website</a> for more info.</p>
<p>P.S. I am using gnuplot v4.2 while writing this.</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/404/python-generating-graphs-with-matplotlib/" rel="bookmark" title="Permanent Link: Python: Generating graphs with matplotlib" >Python: Generating graphs with matplotlib</a></span><div class="aizatto_related_posts_excerpt">Matplotlib is a python 2D plotting library that can produce dots lines, histograms, power spectra, bar charts, pie chart...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/170/plotting-spice-data/" rel="bookmark" title="Permanent Link: Plotting SPICE Data" >Plotting SPICE Data</a></span><div class="aizatto_related_posts_excerpt">Last post, I only intro how we can save the data and plot in text files. Actually we can plot the SPICE data in graphica...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/250/a-graphical-disk-usage-analyzer/" rel="bookmark" title="Permanent Link: a graphical disk usage analyzer" >a graphical disk usage analyzer</a></span><div class="aizatto_related_posts_excerpt">Ubuntu Feisty Fawn has improves in graphics and user friendliness, one of the tools install by default is baobab. Baobab...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/znvgp2McOqjAI1B3zL9ZJoc93Lw/0/da"><img src="http://feedads.g.doubleclick.net/~a/znvgp2McOqjAI1B3zL9ZJoc93Lw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/znvgp2McOqjAI1B3zL9ZJoc93Lw/1/da"><img src="http://feedads.g.doubleclick.net/~a/znvgp2McOqjAI1B3zL9ZJoc93Lw/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/dYHjk8gmXlM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/487/plot-your-graphs-with-command-line-gnuplot/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/487/plot-your-graphs-with-command-line-gnuplot/</feedburner:origLink></item>
		<item>
		<title>Python: Manipulate string or binary bytes with StringIO</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/femVfNGdStg/</link>
		<comments>http://linux.byexamples.com/archives/483/python-manipulate-string-or-binary-bytes-with-stringio/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 00:55:50 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Developer]]></category>

		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=483</guid>
		<description><![CDATA[Sometimes it is not convenient to construct string using equal (=) like this:
str = "Hello, "
...
str = str + "my name is "
...
str = str + Name
print str
In python, we have string stream (StringIO) that will behave like file stream, you can construct your string like this:
str=StringIO()
...
str.write("Hello, ")
...
str.write("my name is ")
...
str.write(Name)
print str.getvalue()
The same way, you [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is not convenient to construct string using equal (=) like this:</p>
<pre><code>str = "Hello, "
...
str = str + "my name is "
...
str = str + Name
print str</code></pre>
<p>In python, we have string stream (StringIO) that will behave like file stream, you can construct your string like this:</p>
<pre><code>str=StringIO()
...
str.write("Hello, ")
...
str.write("my name is ")
...
str.write(Name)
print str.getvalue()</code></pre>
<p>The same way, you can construct your binary bytes with StringIO and write it into file once you are done.</p>
<pre><code>
bin=StringIO()
bin.write("/x5F/x5F%c" % 0xFF)
...
file = open ("my.bin","wb")
file.write(bin.getvalue())
file.close()</code></pre>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/478/python-writing-binary-file/" rel="bookmark" title="Permanent Link: python: writing binary file" >python: writing binary file</a></span><div class="aizatto_related_posts_excerpt">Python is the best scripting language which I found it out perform Bash script as well as Lua. I like python's scripting...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/97/reverse-a-string/" rel="bookmark" title="Permanent Link: Reverse a string" >Reverse a string</a></span><div class="aizatto_related_posts_excerpt">python provides a -c option to accept and run a script line.

To reverse a string like this one: "toydi deified idiot"...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/364/python-manipulate-date-and-time-variables/" rel="bookmark" title="Permanent Link: Python: Manipulate Date and Time variables" >Python: Manipulate Date and Time variables</a></span><div class="aizatto_related_posts_excerpt">When comes to data time related calculations, we usually calculate for time difference, for example How long is the down...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/_tA68qlEx7iCII-eDsamoC-KFGI/0/da"><img src="http://feedads.g.doubleclick.net/~a/_tA68qlEx7iCII-eDsamoC-KFGI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_tA68qlEx7iCII-eDsamoC-KFGI/1/da"><img src="http://feedads.g.doubleclick.net/~a/_tA68qlEx7iCII-eDsamoC-KFGI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/femVfNGdStg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/483/python-manipulate-string-or-binary-bytes-with-stringio/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/483/python-manipulate-string-or-binary-bytes-with-stringio/</feedburner:origLink></item>
		<item>
		<title>python: writing binary file</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/Nw5Y6KSo6GY/</link>
		<comments>http://linux.byexamples.com/archives/478/python-writing-binary-file/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 06:06:34 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[python]]></category>

		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=478</guid>
		<description><![CDATA[Python is the best scripting language which I found it out perform Bash script as well as Lua. I like python&#8217;s scripting syntax, which is make sense and very convenient in string manipulations. Its now come to binary format manipulation, how convenient is python?
I have carry out some research on python into writing binary files. [...]]]></description>
			<content:encoded><![CDATA[<p>Python is the best scripting language which I found it out perform Bash script as well as Lua. I like python&#8217;s scripting syntax, which is make sense and very convenient in string manipulations. Its now come to binary format manipulation, how convenient is python?</p>
<p>I have carry out some research on python into writing binary files. Either writing byte by byte manually, construct a binary stream then write it one short, or convert it from hex string, they are easy to write in python.</p>
<p>To open a file for binary writing is easy, it is the same way you do for reading, just change the mode into &#8220;wb&#8221;.</p>
<pre><code>file = open("test.bin","wb")</code></pre>
<p><strong>But, how to write the binary byte into the file? </strong><br />
You may write it straight away with hex code like this:</p>
<pre><code>file.write("\x5F\x9D\x3E")
file.close()
</code></pre>
<p>Now, check it out with hexedit, </p>
<pre><code>hexedit test.bin</code></pre>
<p>You will see this:</p>
<pre><code>00000000   5F 9D 3E                                                                                                _.>
00000020
00000040
</code></pre>
<p>Now, open the file to append more bytes:</p>
<pre><code>file = open("test.bin","ab")</code></pre>
<p><strong>What if I want to store by bin value into a stream and write it one short?</strong></p>
<pre><code>s ="\x45\xF3"
s = s + "%c%c" % (0x45,0xF3)
file.write(s)
file.close()
</code></pre>
<p><strong>Any convenient ways if I can obtained a hex string, and want to convert it back to binary format?</strong><br />
Yes, you just need to import binascii</p>
<pre><code>import binascii
hs="5B7F888489FEDA"
hb=binascii.a2b_hex(hs)
file.write(hb)
file.close()</code></pre>
<p>Isn&#8217;t it easy? I love python. You may want to discover more functions of binascii with <a href="http://linux.byexamples.com/archives/367/experiencing-with-ipython/">ipython</a>.</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/483/python-manipulate-string-or-binary-bytes-with-stringio/" rel="bookmark" title="Permanent Link: Python: Manipulate string or binary bytes with StringIO" >Python: Manipulate string or binary bytes with StringIO</a></span><div class="aizatto_related_posts_excerpt">Sometimes it is not convenient to construct string using equal (=) like this:
str = "Hello, "
...
str = str + "my nam...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/365/python-convey-the-exception-traceback-into-log-file/" rel="bookmark" title="Permanent Link: python: convey the exception traceback into log file" >python: convey the exception traceback into log file</a></span><div class="aizatto_related_posts_excerpt">Python is the interpreter language, you do not need to compile your code, and also you have no ways to check for your sy...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/69/convert-perl-scripts-into-binaries/" rel="bookmark" title="Permanent Link: convert perl scripts into binaries" >convert perl scripts into binaries</a></span><div class="aizatto_related_posts_excerpt">You can covert perl script into ELF binary files, c source code, bytecode etc. You do this my using perlcc.

You can h...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/cBJKctBOemi--3gKZfLQ8n1eDhw/0/da"><img src="http://feedads.g.doubleclick.net/~a/cBJKctBOemi--3gKZfLQ8n1eDhw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/cBJKctBOemi--3gKZfLQ8n1eDhw/1/da"><img src="http://feedads.g.doubleclick.net/~a/cBJKctBOemi--3gKZfLQ8n1eDhw/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/Nw5Y6KSo6GY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/478/python-writing-binary-file/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/478/python-writing-binary-file/</feedburner:origLink></item>
		<item>
		<title>Bash script that process input from pipeline or file redirection</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/6ughQZW3qJs/</link>
		<comments>http://linux.byexamples.com/archives/474/bash-script-that-process-input-from-pipeline-or-file-redirection/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 02:20:09 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Bash]]></category>

		<category><![CDATA[pipeline]]></category>

		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=474</guid>
		<description><![CDATA[I remember I wrote a post regarding how to makes python processing string from the pipeline stream. This time, I find the needs to do it in bash script.
I created a hex string splitter script for my friend who needs to decode the raw data that will be written in hex string such as &#8220;5F443D95FEA3D4787AEDC4&#8243;. [...]]]></description>
			<content:encoded><![CDATA[<p>I remember I wrote a post regarding <a href="python: handle string from pipelines and list of param">how to makes python processing string from the pipeline stream</a>. This time, I find the needs to do it in bash script.</p>
<p>I created a hex string splitter script for my friend who needs to decode the raw data that will be written in hex string such as <strong>&#8220;5F443D95FEA3D4787AEDC4&#8243;</strong>. Every time, he split them byte by byte manually into this form (<strong>&#8220;5F 44 3D 95 FE A3 D4 78 7A ED C4&#8243;</strong>) for better reading. He was complain that the manual process was always bothering him. I told him that can be done by writting a simple bash script.</p>
<p>My hexsplit.sh will do this:</p>
<pre><code>./hexsplit.sh "5F443D95FEA3D4787AEDC4"</code></pre>
<p>But what if he grep this hex string from other file? Or what if the hex string are stored in a file? I want my bash script to be able to process hex string from pipeline stream as well as file redirection too, like this:</p>
<pre><code> grep "hex" dummy.log | cut -d"]" -f2 | ./hexsplit.sh</code></pre>
<p>or like this:</p>
<pre><code> ./hexsplit.sh &lt; dummy.log</code></pre>
<p>How to write this hexsplit.sh?</p>
<p>hexsplit.sh</p>
<pre><code>
#!/bin/bash

if [ -e $1 ] ;then read str; else str=$1;fi
len=`expr length $str`
for (( a=0; a<=$len; a=a+2 )); do echo -n ${str:a:2}" "; done
echo ""
</code></pre>
<p>The important line is the <strong>if statement</strong>. If there are no argument specified ( -e $1), I <strong>read</strong> from the stream, else i take it from $1 ( param 1). It is so simple isn&#8217;t it?</p>
<p>Have fun!</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/343/python-handle-string-from-pipelines-and-list-of-param/" rel="bookmark" title="Permanent Link: python: handle string from pipelines and list of param" >python: handle string from pipelines and list of param</a></span><div class="aizatto_related_posts_excerpt">While I was searching ways to implement pipeline input for my python apps, I read an article from linuxjournal.com that ...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/236/bash-timed-read-input/" rel="bookmark" title="Permanent Link: Bash: timed read input" >Bash: timed read input</a></span><div class="aizatto_related_posts_excerpt">For some critical task, you just can't wait for user response forever. If the user do not respond to input after certain...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/165/set-the-default-xinput-to-scim-using-im-switch/" rel="bookmark" title="Permanent Link: Set the default xinput to scim using im-switch" >Set the default xinput to scim using im-switch</a></span><div class="aizatto_related_posts_excerpt">If you are a chinese writer, or you want to use chinese input under english locale ( default locale ). Usually people us...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/-NuozocIOvvON4EOZPTPxGqoYnc/0/da"><img src="http://feedads.g.doubleclick.net/~a/-NuozocIOvvON4EOZPTPxGqoYnc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/-NuozocIOvvON4EOZPTPxGqoYnc/1/da"><img src="http://feedads.g.doubleclick.net/~a/-NuozocIOvvON4EOZPTPxGqoYnc/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/6ughQZW3qJs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/474/bash-script-that-process-input-from-pipeline-or-file-redirection/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/474/bash-script-that-process-input-from-pipeline-or-file-redirection/</feedburner:origLink></item>
		<item>
		<title>List command line history with timestamp</title>
		<link>http://feedproxy.google.com/~r/LinuxByExamples/~3/5JqAAu8OjxI/</link>
		<comments>http://linux.byexamples.com/archives/467/list-command-line-history-with-timestamp/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 18:23:02 +0000</pubDate>
		<dc:creator>mysurface</dc:creator>
		
		<category><![CDATA[Common]]></category>

		<category><![CDATA[history]]></category>

		<category><![CDATA[last]]></category>

		<guid isPermaLink="false">http://linux.byexamples.com/?p=467</guid>
		<description><![CDATA[History is a common command for shell to list out all the executed commands. It is very useful when it comes to investigation on what commands was executed that tear down the server. With the help of last command, you be able to track the login time of particular user as well as the the [...]]]></description>
			<content:encoded><![CDATA[<p>History is a common command for shell to list out all the executed commands. It is very useful when it comes to investigation on what commands was executed that tear down the server. With the help of <strong>last</strong> command, you be able to track the login time of particular user as well as the the duration of the time he/she stays login.</p>
<pre><code>last
...
mysurface    tty7         :0               Mon Oct  6 20:07 - down   (00:00)
reboot   system boot  2.6.24.4-64.fc8  Mon Oct  6 20:06          (00:00)
mysurface    pts/8        10.168.28.44     Mon Oct  6 17:42 - down   (01:58)
mysurface    pts/7        :0.0             Mon Oct  6 17:41 - 19:40  (01:59)
mysurface    pts/6        :0.0             Mon Oct  6 17:27 - 19:40  (02:13)
mysurface    pts/5        :0.0             Mon Oct  6 17:27 - 19:40  (02:13)
mysurface    pts/5        :0.0             Mon Oct  6 15:52 - 15:59  (00:07)
...</code></pre>
<p>If the command line history could provides the date time of the commands being executed, that may really narrow down the scope of the user actions that cause the server malfunction. By default, history do not append with timestamp, but it is easy to configure it to display timestamp, you just need to set one environment variable <strong>HISTTIMEFORMAT</strong>.</p>
<p><strong>HISTTIMEFORMAT</strong> takes format string of <strong>strftime</strong>. Check out the strftime manual to choose and construct the timestamp that suit your taste. My favorite is &#8220;%F %T &#8220;.</p>
<pre><code>export HISTTIMEFORMAT="%F %T "</code></pre>
<p>Execute <strong>history</strong> again and you will see the effect on the spot, bare in mind that the timestamp for command lines that executed at previous sessions may not valid, as the time was not tracked.</p>
<pre><code>...
  994  2008-10-16 02:27:40 exit
  995  2008-10-16 01:12:20 iptables -nL
  996  2008-10-16 01:47:46 vi .bash_profile
  997  2008-10-16 01:47:55 history
  998  2008-10-16 01:48:03 . .bash_profile
  999  2008-10-16 01:48:04 history
 1000  2008-10-16 01:48:09 exit
 1001  2008-10-16 02:27:43 history
...</code></pre>
<p>I would suggest you to put the export into ~/.bash_profile as well as /root/.bash_profile. In case you do not have .bash_profile, you can choose to put into ~/.bashrc.</p>
<p>Don&#8217;t mess up my servers! Your actions will be track!</p>
<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><br/><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/38/retrieve-back-command-been-enter/" rel="bookmark" title="Permanent Link: Retrieve Back Command Been Enter" >Retrieve Back Command Been Enter</a></span><div class="aizatto_related_posts_excerpt">Everytime you enter a command in shell. It will be store it a file for later use, so  you can retrieve back the command ...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/332/what-is-your-10-common-linux-commands/" rel="bookmark" title="Permanent Link: what is your 10 common linux commands?" >what is your 10 common linux commands?</a></span><div class="aizatto_related_posts_excerpt">Dear command line ninjas, Mr CLI and keyboard pianist:

What is your regular command you use? I am sure you must think...</div><p></p><span class="aizatto_related_posts_title" ><a href="http://linux.byexamples.com/archives/503/sometimes-grep-alone-is-enough-to-do-the-task/" rel="bookmark" title="Permanent Link: sometimes, grep alone is enough to do the task" >sometimes, grep alone is enough to do the task</a></span><div class="aizatto_related_posts_excerpt">One thing that makes UNIXes command line so powerful is because of pipelines. Pipelines is a set of processes chained by...</div><p></p></div>
<p><a href="http://feedads.g.doubleclick.net/~a/KgpU90fstxz3c56K66c3KmaqeMA/0/da"><img src="http://feedads.g.doubleclick.net/~a/KgpU90fstxz3c56K66c3KmaqeMA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/KgpU90fstxz3c56K66c3KmaqeMA/1/da"><img src="http://feedads.g.doubleclick.net/~a/KgpU90fstxz3c56K66c3KmaqeMA/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/LinuxByExamples/~4/5JqAAu8OjxI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://linux.byexamples.com/archives/467/list-command-line-history-with-timestamp/feed/</wfw:commentRss>
		<feedburner:origLink>http://linux.byexamples.com/archives/467/list-command-line-history-with-timestamp/</feedburner:origLink></item>
	</channel>
</rss>
