Monday, October 18, 2010

Wait-free by using libnotify under Linux

Under Linux, suppose you are to execute a command that takes a long time to finish. You either have to check the status again and again or just sit there daring at the flickering terminal. An alternative is to let system pop up an image to notify you, thanks to libnotify and its Python binding.

Below is the script, name it as "note" and put it in your PATH. Suppose your command is "locate nf.ml", then just type "locate nf.ml;note" and it works as we want! (You do need to install Gentoo "dev-python/notify-python" or Ubuntu python-notify first and change "file:///usr/bin/note.png" below to your favorite image)
#!/usr/bin/env python
import os,sys
try:
    import pynotify
    if pynotify.init("My Application Name"):
    title = os.uname()[1]
    try:
        message = sys.argv[1]
    except:
        message = 'note'
    home = os.environ['HOME']
        n = pynotify.Notification(title, message, "file:///usr/bin/note.png")
    #n.set_timeout(pynotify.EXPIRES_NEVER)
        n.show()
    else:
        print "there was a problem initializing the pynotify module"
except:
    print "you don't seem to have pynotify installed"

Sunday, October 17, 2010

Linux survival sheet (everyday usage)

count the disk usage
       du -a --max-depth=2 |sort -n >~/t
see the real(wall clock)/user(user space time)/sys(kernel space time) 
       time ./cmd
see what processes are using a file
       fuser
decide which files need to be executed at system booting
       rcconf
show hardware info
       lshw
       lspci
       x86info -a
show cpu/mem info
       cat /proc/cpuinfo
       cat /proc/meminfo
show hostname
       hostname
       uname -n
list all files under current directory containing 'fb.mid' in name
       du -a|grep fb.mid|cut -f2
list all with relative path
       du -a|cut -f2
list all with absolute path
       du -a `pwd`|cut -f2
get the top 20 recent modified files
       ls -lt| head -n 20
extract filename by column
       ls -l| awk '{print $9}'
create parent dir automatically
       mkdir -p /a/b/c
       cp --parents /a/b d
       (not -p)       
create soft link
       ln -s target linkname
easier browsing
       pushd .
       popd
'5 4 * * * updatedb' will exec updatedb every 4:05 am.
       su -;crontab -l
for restart after every reboot:
       @reboot nohup cmd >/dev/null
less frequent system monitoring
       top -d 30
online watching what others are doing
       watch w
send message to all logged on users as root
       wall
talking on console
       mesg y
       write root
being listed in /etc/host.conf on server side will speedup your login
check disk partitions
       fdisk -l /dev/sda
test disk read/write speed
       hdparm -tT /dev/sda1
after system crashes and DBUS fails to work
       eval `dbus-launch --sh-syntax --exit-with-session`
to reduce the file system overhead of updating access time
       mount -o relatime
match process
       pgrep 
       pkill 
       pidof
make a deb from source install (an alternative is dh_make)
       checkinstall -D make install
       checkinstall -D --strip=no --stripso=no make install
       (remove with dpkg -r `dpkg-deb -W <deb name>`)
For Firefox, it is
       checkinstall -D make install -f client.mk
show address mapping of a process
       pmap
various statistics
       iostat
       vmstat

Linux survival sheet (Bash)

^u   erase command line to head
^w   erase last word typed
^Z   send a task to background
^d   quit terminal
fg [num]  revoke the [num] job from background.
cp file{,.old}
is shorthand for
     cp file file.old
export HISTIGNORE="&:ls:[bf]g:exit"
export HISTCONTROL=erasedups
export HISTFILESIZE=10000
export PS1='\u@\h:\w \A$'
set 1 second cap for execution (don't do this in your bash!)
        ulimit -t 1;./a.out
enable core dump for debugging
        ulimit -c unlimited
Restore garbled terminal after accidentally print a binary
        Press [ctrl-v] [ctrl-o] [return]

Install Haskell environment on Gentoo

I just installed Haskell environment (ghc 6.12.3) on a Gentoo Linux system(2.6.31-gentoo-r10). Since the package system of Gentoo is not quite compatible with cabal, I choose to install the binary version of ghc and then use cabal to install everything else. The first problem is a complain going like this: "gtk2hsC2hs is required but it could not be found". It turns out that we need to cabal install gtk2hs-buildtools before moving on.

Another complain repetitively made by cabal is "can't load .so/.DLL for: readline (/usr/lib/libreadline.so: invalid ELF header)", easily reproducible by issuing "ghci -package readline". After some investigation, it turns out to be due to a Gentoo policy.

To work around the problem, I assumed root privilege to use the following script, which basically restores libraries under /usr/lib/ to be true ELF files instead of symbolic links.

#!/bin/env python
import os
cmd = "mv /usr/lib/lib%s.so{,.old};ln -s /lib/lib%s.so"%(i,i)
for i in ["ncurses","ncursesw","readline","z"]:os.system (cmd)

Lazy list is Python generator

Functional programmers often miss lazy list when coding in a popular language. In some circumstances, they can make do with Python generators. An even better news is that from 2.3, Python provides  itertools library, which contains the old friends: cycle, repeat, takeWhile, dropWhile ... such that
takeWhile (<10) [1..]
translates to
>>> list(takewhile (lambda x:x<10,count(1)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Now if the library is short for your need, we can resort to yield statements in python. Suppose we want to enumerate ways of splitting a list into two parts.
>>> list(splits ([1,2,3]))
[([1, 2, 3], []), ([2, 3], [1]), ([1, 3], [2]), ([3], [1, 2]), ([1, 2], [3]), ([2], [1, 3]), ([1], [2, 3]), ([], [1, 2, 3])]
In Haskell it will be
splits [] = [([],[])]
splits (x:xs) = concatMap expand $ splits xs
                where expand (a,b) = [(x:a,b),(a,x:b)])
In Python it will be
def splits(l):
    if l==[]:yield ([],[])
    else:
        x,xs=l[0],l[1:]
        for (a,b) in splits(xs):
            yield ([x]+a,b)
            yield (a,[x]+b)
Quite literate, isn't it!