Wednesday, May 21, 2008

A bash script to keep me online

Most people I know don't use the Internet to its full potential. And they still complain about their ISP. People like me, who practically live on food, clothing, shelter and the internet have every right to complain. Despite having an unlimited connection, my ISP decides to kick me off the net ever so often, especially when I leave a large download running at night. Its a daily affair, and I needed a solution.

I already have two shell scripts to conveniently get on and off the net, which I call online.sh and offline.sh . Sify, my ISP, provides a graphical login program. This means that I cannot login if I don't have the X server running. Its a bad idea to have a login program in the first place, and even worse that it needs the X server. Fortunately, there are alternatives out there, and I use SuperSify instead.

So the /usr/local/bin/online.sh script is as simple as:

#!/bin/bash
cd /media/hda1/supersify/
./ss.sh -u your_username -p xxxx -m 00:11:B6:22:BB:F7

Make sure you chmod it to deny rw permissions to other users. I would have preferred to encrypt the password, but I don't know how. (replace the mac address with whatever you mac address is. Run ifconfig to find out)

Now comes the script which checks if you're online, and connects you if you're not. It just pings google.
/usr/local/bin/keeponline.sh

#!/bin/bash
ping -c 1 -W 60 www.google.com

if [ "$?" = "0" ]; then
echo "You are connected"
else
/usr/local/bin/online.sh
echo "Disconnect detected at `date`, reconnecting" >> /home/akudewan/logs/keeponline.log
fi


Unfortunately, the log file can grow too large. So there's yet another script to reset the log file every month.
/usr/local/bin/resetlog.sh

#!/bin/bash
mv /home/akudewan/logs/keeponline.log /home/akudewan/logs/keeponline.log.0

Needless to say, this is a very dirty fix, so you can skip the log file altogether if you wish.

Now we need to run two shellscripts as cron jobs - keeponline.sh and resetlog.sh.
Run the command:

crontab -e

This will edit/create a crontab file for the user you're logged in as.

Add the following lines to this file:

# m h dom mon dow command
0,10,20,30,40,50 * * * * /usr/local/bin/keeponline.sh 1> /dev/null
* * 1 * * /usr/local/bin/resetlog.sh

This means that keeponline.sh will be run every 10 minutes, and resetlog.sh will be run on the 1st of every month.
(Run crontab -l to see your crontab)

Thats it! Now my ISP can't kick me off for more than 11 minutes :)

Saturday, May 17, 2008

Always read your log files

Whenever there is a new Ubuntu release, there's one part of the world that thinks that it is the worst upgrade ever, and the other part of the world that thinks that its the best thing that ever happened. Its the same with every distro, I guess.

After upgrading to Hardy, I thought it was the worst upgrade ever. The system was unstable, buggy, and programs would crash randomly. This was until I read my log files after an X server crash.

from /var/log/syslog :
May 8 12:34:08 ranjandesk -- MARK --
May 8 12:46:35 ranjandesk gdm[5796]: WARNING: gdm_slave_xioerror_handler: Fatal X error - Restarting :0
May 8 12:46:51 ranjandesk kernel: [ 6802.873564] Bad page state in process 'kdesktop'
May 8 12:46:51 ranjandesk kernel: [ 6802.873565] page:c1a04120 flags:0x80000000 mapping:00000000 mapcount:-285212672 count:0
May 8 12:46:51 ranjandesk kernel: [ 6802.873566] Trying to fix it up, but a reboot is needed
May 8 12:46:51 ranjandesk kernel: [ 6802.873567] Backtrace:
May 8 12:46:51 ranjandesk kernel: [ 6802.873591] [bad_page+0x63/0xa0] bad_page+0x63/0xa0
May 8 12:46:51 ranjandesk kernel: [ 6802.873620] [get_page_from_freelist+0x343/0x3a0] get_page_from_freelist+0x343/0x3a0
May 8 12:46:51 ranjandesk kernel: [ 6802.873662] [agpgart:__alloc_pages+0x4f/0x340] __alloc_pages+0x4f/0x340
May 8 12:46:51 ranjandesk kernel: [ 6802.873684] [anon_vma_prepare+0x1d/0xe0] anon_vma_prepare+0x1d/0xe0
May 8 12:46:51 ranjandesk kernel: [ 6802.873687] [loop:kunmap_atomic+0x6a/0xa0] kunmap_atomic+0x6a/0xa0
May 8 12:46:51 ranjandesk kernel: [ 6802.873699] [__handle_mm_fault+0x8c7/0xb00] __handle_mm_fault+0x8c7/0xb00
May 8 12:46:51 ranjandesk kernel: [ 6802.873713] [snd_pcm:getnstimeofday+0x36/0x96b0] getnstimeofday+0x36/0xd0
May 8 12:46:51 ranjandesk kernel: [ 6802.873732] [sched_clock+0x1a/0x70] sched_clock+0x1a/0x70
May 8 12:46:51 ranjandesk kernel: [ 6802.873755] [do_page_fault+0x126/0x690] do_page_fault+0x126/0x690
May 8 12:46:51 ranjandesk kernel: [ 6802.873792] [do_page_fault+0x0/0x690] do_page_fault+0x0/0x690
May 8 12:46:51 ranjandesk kernel: [ 6802.873798] [error_code+0x72/0x80] error_code+0x72/0x80
May 8 12:46:51 ranjandesk kernel: [ 6802.873818] [clip_ioctl+0x3a0/0x510] clip_ioctl+0x3a0/0x510
May 8 12:46:51 ranjandesk kernel: [ 6802.873836] =======================


Bad Page? I know the next thing to do. Check the RAM! I ran the memtest86+ thingy that comes with Ubuntu. And sure enough, it showed errors in Test 5. The RAM chip works fine in the first slot, so it would seem that the problem is with the second slot. Since the mobo and RAM are still under warranty, I can get this fixed before its too late.

I wouldn't even know that there was something wrong with my RAM slot if I hadn't read my log files. I remember in my old machine, the graphics card fan was bust, and it would start running in "modulated clock mode". The log files told me.

A few days ago, I was getting random telnet attempts from IPs in Korea and China. Again, it was in the log files. Most probably botnets. I decided to remove telnetd and just stick to ssh instead.

Even if you don't know what the log files mean, doing a Google can show a few results with bugs or solutions to the problem.

Friday, May 02, 2008

rsync


rsync is probably the simplest way to sync data between two computers, or even the same computer. Now that I have plenty of storage on my desktop, I can take backups from the laptop. So I tried out rsync.

There are a lot of howtos out there, telling you to schedule backups and stuff. But they seemed somewhat complicated, and I didn't really want scheduling...The laptop runs at irregular times.

So whats the simplest, no-shit solution for rsync? Its grsync!

Ok, then I saw what commands its running on the terminal, and made a shellscript :)

On Ubuntu, install grsync by running: sudo apt-get install grsync