Saturday, December 30, 2006

Screen Utility for Linux

In unix like operating systems we have the 'screen' utility,
so usefull as easy to use. In cases where you need to
compile a kernel in the background in a remote server
(where the connection can get lost), download a file or
do some critical operation without the risk of loosing
the network connection to the server, the 'screen' utility
comes handy.

Run it just writing its name:

This creates a virtual shell, a virtual terminal,
for those (like me) that love virtualization :-)
Now you see a new shell, type some commands,
leave a 'top' running, and type:

ctrl+a+c (this creates a new window, like a new shell).

How to go back to the window with the 'top' ?
Type ctrl+a+n (this jumps to the next window,
in this case, the first one).

I dont know of a limit of shells you can
create with "ctrl+a+c".
You jump backward to the previous window
with "ctrl+a+p".
Split the current 'screen' window in multiple
windows with "ctrl+a+S"
Jump between splitted windows with "ctrl+a+TAB".

And, if you need to send the the terminal the "ctrl+a" signal, press "ctrl+a+a".

Now... the real magic, how to leave this process running in the background and how to recover the terminal after a connection loose ?

Press ctrl+a+d to disconnect from 'screen' and leave
all the terminals in the background, these processes
will survive as i said, a disconnection, so you can
disconnect from that server and come back in two days
that screen will be still there.

This shows something like:

[root@sriram ~]# screen -ls
There is a screen on:
2131.pts-1.sriram (Detached)
1 Socket in /var/run/screen/S-root.

To re-connect to screen write:
[root@sriram ~]# screen -r 2131.pts-1.sriram

And you are back in the screen terminals.

I hope it was usefull, please leave me a
message with your experience or new tricks.

Sunday, December 24, 2006

Failover routing - ISP switch

a small shell script that ping a test ip and if it is
unreachable switch to the other gateway.
Schedule this script to run every minute or so from cron.

TESTIP="" # Any reliable Internet ip that responds to ping.
CURGW=`/sbin/route -n |awk '/^ {print $2 }'`

if ping -w2 -c3 $TESTIP >/dev/null 2>&1; then
echo "Active ISP is Ok."
if [ "$CURGW" = "$GW1" ]; then
/sbin/route del default
/sbin/route add default gw $NEWGW
This script requires that both your ISP's are pre-configured and are
not messing with your routing table when connected/disconnected,
esp. PPP is know to be quite unpolite when dealing with routes.
You probably want some sort of test to see if the new path is
working as well. Other concerns may be iptables/tc reloading
and stuff like that.