Tuesday, November 12, 2013

How to capture top output to a readable format

For this case, we use batch mode in top. From top's manpage:
Starts  top in 'Batch mode', which could be useful for sending output from top to other programs or to a file.  In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.

To use it is very easy, you must provide -b for batch mode, and -n for number of iteration that you desire. You can pipe it to less, tail, head or pipe it to a file, according to your need.

foo@thorium:~$ top -b -n 1 | head

top - 10:47:15 up 3 days, 23:52,  1 user,  load average: 2.01, 0.99, 0.57

Tasks: 140 total,   2 running, 136 sleeping,   0 stopped,   2 zombie

Cpu(s): 13.2%us,  4.9%sy,  0.0%ni, 81.6%id,  0.3%wa,  0.0%hi,  0.0%si,  0.0%st

Mem:   1025608k total,   865376k used,   160232k free,    80332k buffers

Swap:   522236k total,    66468k used,   455768k free,   273996k cached


  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                              
1192 root      20   0  122m  65m  10m R 13.8  6.6 559:31.01 Xorg                                                                                                  
19333 foo   20   0  234m  61m  13m S  9.8  6.2 626:59.69 nxplayer.bin                                                                                            
19440 foo   20   0  175m  14m 9612 S  7.9  1.5   0:44.78 lxterminal  

The above command will list 10 lines of top output in batch mode, and being set to iterate only once.



Monday, November 11, 2013

Autospawn screen session on ssh connection

This is particularly useful in a situation where your ssh connection always get disconnected, and you have no privilege to tweak the ssh server settings to avoid that.


  1. First method, simple and easy, but will create multiple screens if you have more ssh sessions: 
    echo "screen -R" >> ~/.bash_profile; source ~/.bash_profile
  2. 2nd method, smarter than the above, will attach to the existing screen session if there is any, and create new one if none is available 
    echo "if $(screen -ls | grep -q pts); then screen -x; else screen -R; fi" >> ~/.bash_profile; source ~/.bash_profile

Tuesday, September 24, 2013

Changing background color theme in vim

Sometimes, when you open a document in vim, the color of the font and background is not easy on the eye, like below:













In order to encounter this, you can change the backgroud of vim to use light background, by using
:set background=light
or
:set bg=light
in vim to become like below:












You can change the backgound back to dark by using
:set background=dark
or
:set bg=dark
in vim.

That's all, hope this is going to be useful to you.


Monday, August 26, 2013

How to extract deb file without having dpkg

.deb files are regular ar archives. You can manipulate the file by using ar command.

For example, I have a package called lynx_2.8.8dev.15-2_all.deb in a centos box.

$ cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m

$ ls -lh
total 4.0K
-rw-rw-r--. 1 foo foo 3.9K Jan 18  2013 lynx_2.8.8dev.15-2_all.deb

To extract the file, just run ar x <.deb.filename>
$ ar x lynx_2.8.8dev.15-2_all.deb
You will see 3 files being extracted, control.tar.gz, data.tar.gz and debian binary.
$ ls
control.tar.gz  data.tar.gz  debian-binary  lynx_2.8.8dev.15-2_all.deb

debian-binary is a text file containing deb packaging version number.
$ cat debian-binary 
2.0

control.tar.gz contained control, preinst and md5sums.
$ tar -tf control.tar.gz ./
./md5sums
./control
./preinst

data.tar.gz is a compressed file containing the files which are going to be installed on the system.
$ tar -tf data.tar.gz ./
./usr/
./usr/share/
./usr/share/doc/
./usr/share/doc/lynx/
./usr/share/doc/lynx/copyright
./usr/share/doc/lynx/changelog.Debian.gz

Wednesday, July 10, 2013

Print to pdf from command

To enable this, there are 2 packages that you need to install, which are cups and cups-pdf. This has been tested on CentOS 5.5.

# yum install cups cups-pdf -y

Once installed, check which printer is available:

$ lpstat -p -d 

printer Cups-PDF is idle.  enabled since Wed 10 Jul 2013 09:30:05 AM EST

no system default destination

where lpstat is to show cups status information, -p is to tell lpstat to list all printers available and -d is to show the current default destination. In this case, this system has one printer installed, which is Cups-PDF.

To print, lpr command is needed:

$ echo "Printing some characters to a pdf file" | lpr -P Cups-PDF -J print01.pdf

where -P is to specify the printer name that you want to use, and -J is for the output file name.

The file will be saved in ~/Desktop, but this can be changed in /etc/cups/cups-pdf.conf.

$ ls ~/Desktop/

print01.pdf