Thursday, January 23, 2014

Sort IP Address

There is time you need to sort IP address in the way human understand.
# sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 addresses.txt

Where,
  • -t . : Set field to . (dot) as our IPs separated by dot symbol
  • -n : Makes the program sort according to numerical value
  • -k opts: Sort data / fields using the given column number. For example, the option -k 2 made the program sort using the second column of data. The option -k 3,3n -k 4,4n sorts each column. First it will sort 3rd column and then 4th column.

Friday, January 17, 2014

Report A Snapshot of Current Processes

ps [options]

ps displays information about a selection of the active processes. It accepts several kinds of options:

  1. UNIX options, which may be grouped and must be preceded by a dash.
  2. BSD options, which may be grouped and must not be used with a dash.
  3. GNU long options, which are preceded by two dashes.

 To see every process on the system using standard syntax:
ps -eps -efps -eFps -ely

 To see every process on the system using BSD syntax:
ps axps axu

 To print a process tree:
ps -ejHps axjf

 To get info about threads:
ps -eLfps axms

 To get security info:
ps -eo euser,ruser,suser,fuser,f,comm,labelps axZps -eM

 To see every process running as root (real & effective ID) in user format:
ps -U root -u root u

To see every process with a user-defined format:
ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,commps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,commps -eopid,tt,user,fname,tmout,f,wchan

Print only the process IDs of syslogd:
ps -C syslogd -o pid=

Print only the name of PID 42:
ps -p 42 -o comm=

Thursday, January 16, 2014

Shows Exit Code of Previous Command in zsh

To display the exit code of the previous command is very useful in zsh.
PS1='%n%m %~ %(?..[%?] )%# '

%(?..[%?] )
It's a conditional expression. The part before the 1st dot is the expression; the part between the two dots is output if it's TRUE (or 0); the part after the second dot is output if it's FALSE (or any number but 0).

See my full customization on .zshrc file.

Wednesday, January 15, 2014

Tips on GNU Screen

GNU Screen is a simple but powerful terminal multiplexer.

Two basic commands that I use often:
  • screen -r : Re-attach the only-one screen session.
  • screen -ls : To show running sessions 

Here's the features that I use often:
  • ctrl-a + d : To detach the terminal.
  • ctrl-a + c : To create a new terminal
  • ctrl-a + p : To switch to the previous terminal
  • ctrl-a + n : To switch to the next terminal
Here's the features that I use less often:
  • ctrl-a + x : To lock the current terminal
  • ctrl-a + S : To split the terminal to two separate areas/tabs.
  • ctrl-a + X : To close the current area/tab.
  • ctrl-a + [tab] : To switch to the next tab.
  • ctrl-a + H : To create a running log of the session.
Advanced usages:
  • ctrl-a + M : To start/stop monitor for activity
  • ctrl-a + _ : To start/stop monitor for silence 
  • ctrl-a + " : To quickly go to that window
References:
  • http://polishlinux.org/howtos/screen-tips-tricks/
  • http://www.softpanorama.org/Utilities/screen.shtml
  • https://wiki.archlinux.org/index.php/GNU_Screen
  • http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
Tips:
In you accidentally press "ctrl-a s" (instead of "ctrl-a S") while creating tab, which freeze the parent terminal, just press "ctrl-a q" to unfreeze it. 

Thursday, January 09, 2014

Some Tricks in using zsh

These are part of the tricks that I like most about zsh:

ESC-. [ Escape period ]
Inserts the last argument of the previous history line, repeat to go back in history.

ESC-'  [ Escape single-quote ]
To quotes the whole line. (Useful for su -c or ssh).

ESC-q [ Escape q ]
To clears the line and inserts it again on the next prompt, allowing you to issue an interim command.

ESC-RETURN [ Escape ENTER ]
To inserts a literal newline, so you can edit longer commands easily.

References:

  • http://chneukirchen.org/blog/archive/2008/02/10-zsh-tricks-you-may-not-know.html
  • http://www.rayninfo.co.uk/tips/zshtips.html
  • http://grml.org/zsh/zsh-lovers.html

Wednesday, January 08, 2014

AutoCorrection in zsh

By default, zsh comes with autocorrection.

If you found that it is very annoying, you can turn it off separately. To switch it off completely:
unsetopt correct  # newer version
unsetopt correct-all # older version

To switch it off for certain command, such as 'vim', do this in your .zshrc
alias vim='nocorrect vim' 

To switch it off for another copy of zsh:
NOCOR=1 zsh

Monday, January 06, 2014

Check Temperature in Raspberry Pi

Two ways how I check the temperature in my Raspberry Pi.

As normal user:
% cat /sys/class/thermal/thermal_zone0/temp
49768
% /opt/vc/bin/vcgencmd measure_temp
VCHI initialization failed

As sudo/root:
% sudo /opt/vc/bin/vcgencmd measure_temp                
temp=49.8'C

Customize zsh for PiBang

By default, PiBang comes with zsh as the default shell.

After few days using zsh, I found that I have key binding issue. This reminds me that I need to setup key sequence and binding manually for certain keys: HOME, END, and DELETE.

So refer to setup key sequence and bindings manually.

Sunday, January 05, 2014

Check NTP Config in RaspberryPi

Raspberry Pi has no real time clock (RTC). If you power up your RPi without Internet access, you will have wrong time/date.

RPi gets the time/date from NTP servers (a time server). To perform a quick check on your NTP servers:
ntpq -p

Friday, January 03, 2014

Fixed IP on RaspberryPi

I prefer fixed IP to dynamic IP address. So I just changed from dhcp to static in the /etc/network/interface file.
/etc/network/interface:
auto lo
iface lo inet loopback  
auto eth0iface eth0 inet static 
address 192.168.1.11 
netmask 255.255.255.0 
network 192.168.1.0 
broadcast 192.168.1.255 
gateway 192.168.1.1
And don't forget your DNS setting. Once you no longer get network configuration from DHCP server, you need to configure your DNS manually.

/etc/resolv.conf:
nameserver 192.168.1.1

/etc/network/interface and ifconfig output

Hi PiBang (Why I switch to PiBang)

Few reasons:

  • It has server edition. 
  • It uses SystemD (instead of SysV init).
  • It uses zsh (instead of bash).
At the same time:
  • In case I need a desktop environment, instead of LXDE, I still can have i3 (tiling WM) or OpenBox.
  • It still compatible with Rasbian sources.
Interested? Give PiBang a try now.

Reinstalling My PiBang

Today I download the latest PiBang Server edition, pibang-server-systemd-20131119.img (500++ MB and MD5: B824E42A BFCE74EF 980613EF 6109A811) Then follow by unzip it and burn the img file to my 4GB micro SD card.

Plug it into my Raspberry Pi. Connect my Raspberry Pi to TV after connect a USB keyboard. Power it once it is done.

The 1st thing it runs is, raspi-setup. Make all the necessary changes including:

  • Expand the file system.
  • Change hostname.
  • adduser pi
  • Split memory: 16MB
  • Enable camera module and SSH server.