Uninterrupted Linux session : screen command

Have you ever faced losing connections to a remote machine (e.g. from a putty) and you happened to be in middle of a script that took ten hours to complete. So what would you do??

Restart the script after re-connecting.

Not anymore…

Linux screen solves this problem with providing a terminal that runs on server and just exported to your putty/Terminal client. In simple terms, you are running your putty on remote machine and watching the output on your local client.

Now, if you client goes down, just chill!

Screen is running your script on remote machine.

To start a screen session:
– Login to remote machine with as you may wish.
– Run $screen
– Do you stuff

Power cut and, no net connection and your client is down.

Once power is back, just re-login to remote machine.
and issue:
$screen -r

It’ll list all screen sessions running on the remote machine. Get attached to one of the session with:

$session -r

Alternative: Move your job to background

Press ctrl+Z on your application’s active console; then do $bg && disown

Done 🙂

>Expect Pexpect: Python module to help you automate interactive tests

>Many a times we encounter writing a code that requires user input to proceed. Testing and validating such an application can become tedious and frustating. Python implements an elegant solution to cut down the effort needed to “talk to application”.

This is called pexpect module, borrowed from Tcl “expect”. Pexpect is a tool for controlling and automoating programs. It simple “fools” the application with an user input. It runs the program and monitors the output. When output matches a given pattern, it respond to application mimicking the human intervention.

Pexpect can be extensively used in testing and automation. I found it particularly useful for interacting with application like ssh, ftp, passwd, telnet etc.

It comes packaged with standard Ubuntu 9.10. The version of Python used by me is:

$python –version
Python 2.6.4rc2

>Half century of technical know-how sharing

>It’s 50th post on this blog. I am happy with my contribution. Learning is a continuous process and I am an active participant. Today’s post will talk of something that I had never imagined to do in my development career: Testing.

Testing of the product is an interesting job, especially when you are a new user. Past two weeks, I am testing the product I’d be developing.

I’m sharing my experience here:

– Don’t test just for sake of doing it; Understand the ecosystem in which your product would be working.
– Take generous logs of your testing.
– Keep an eye on what you can improve: Be it a process or a feature.
– Try to automate processes if possible.
– Try feeding weird input to your product.
– Testing is learning. You become a very good user of your product.

>Bash Command fo you

>Once again, I am sharing a few good commands with you.

a) Run command in a subshell
$(cd /tmp && ls)

# You will see the contents of /tmp but will stay in PWD only.

b) Reusing cscope database
$cscope -d

c) If you default shell is csh, and you want to move to bash as soon as you login
– Create a file .cshrc in your home directory.
– Edit and add “bash”
– Save and exit.

d) vi can help you see a split screen with two or more files. Try :vsplit.

>Ubuntu 9.10 on Windows XP: Virtual Box

>My office laptop has Windows XP installed and I need Windows for sake of Outlook and Communicator. So I started to look out for alternative for having Linux on my laptop. There were three options suggested by different people:

– Get Ubuntu installed and run Windows XP as a guest on VirtualBox.
Now my workplace’s IT guys do not install Windows on a VM, so they asked me to create an image of the Windows installed on my laptop. I searched for software to accomplish this and tried a few in vain. I tried a software called WinDD to copy Windows image to my portable HDD and since I did not checked thoroughly where it was copying, I got my portable HDD corrupted. It caused me a lot of pain, my important docs and many pics just got shredded.
Well that was it. I dropped this option.

– Get the Ubuntu on a pen-drive(PD) and make your PD boot-able.
It had a problem that Ubuntu installation was not persistent. Many of my setting could not be saved. Though I got a way to create a persistent image, still it can’t save a few things like system time. Verdict: Dropped

– Get the Ubuntu running as a guest on host Windows.
After installing the guest additions, I am very happy with the screen resolution. I got few glitches with network setting and shared folders help. I got over them and will post the details in the next post.

So I am a happy man now, seeing my Ubuntu running on XP. All happy and gay!

PS: I am a fan of VirtualBox and so far have not used any other VM software.

>Torrent: Demystified

>What is a torrent?

A torrent is like a broker between a customer and a seller. It works in a peer-to-peer setup that means a true democracy; all are equal, no master-no slave.

So you need to download a classic movie, which may be on a computer(s), located anywhere across the globe.

How would you know where is it?

Torrent comes to help you here. You google for a torrent of this movie. Download the torrent and voila!.

Torrent has three primary components:

a) Seeders: People that have a copy of the movie you want
b) Leechers: That’s you!
c) Torrent file : It has information about seeders and leechers.

While you are downloading your movie, torrent share the downloaded part with anyone else interested in downloading the same movie. So effectively every leecher becomes a seeder. Now you are even! Hail Democracy!!

>Ubuntu 9.10: Wireless network problem with Broadcom Corporation BCM4312

>Many laptops with Broadcom Corporation BCM4312 802.11b/g hardware shows no response to wireless network with Ubuntu 9.10 Casper.

I could solve this problem with the following method:

a) Go to System-> Administration->Hardware Drivers
Keep your system on Internet(wired ethernet) while doing this.

Install Broadcom 43 and Broadcom STA driver, activate them then.

b) Reboot and see if it work for you.

c) If not, then do the following:

sudo apt-get –purge remove bcmwl-kernel-source
sudo apt-get install patch
sudo apt-get install bcmwl-kernel-source

This should solve the matter. At least it worked for me 🙂

Ref: Ubuntu Forum

>Running Ubuntu 9.10 Casper On your PC

>A complete solution: Image Writing to Installation

Ubuntu is a free open source Linux distribution, primarily for desktop users. There are many reasons to use Ubuntu instead of other Linux distro(Just Google for it).

I am assuming that reader is an Ubuntu user on a x86 system and knows some stuff about Linux.

o) First thing is get the latest Ubuntu bundle(an .iso file) from Ubuntu website.
o) Now get a pen drive.
o) From “Application” menu, run USB creator.
Give the path of iso file and click create USB drive.
+ If pendrive is more than 4GB, You would get an error saying “partition not found”:
Just do the following:
* Install gparted utility with Synopsis Bundle Manger
* Create a partition table with “gparted” on your pendrive
* Now create a FAT32 partition of size 1000MB on this pendrive
o) Run USB startup disk creator again.

o) Voila! You are done

o) You save money spent on CD/DVD to write Ubuntu image.
o) This method is very fast and reliable, compared to use of optical media.

Thread stack in heap (pthreads)

I had known that a thread has a stack. But never thought where is this stack lie in the memory?

Well, it should be process stack. But you can set the thread stack to a heap area.

How to do that?

#include "pthread.h"
#include "stdio.h"
#include "unistd.h"
pthread_mutex_t mutex1;
void* foo(void *arg)
 int arr[1024*8];
 int x = 0;
 while(x++ < (8*1024) )
 { arr[x] = x; }
 puts("Thread created");
int main()
 int *p;
 void *ret; 
 p = malloc(1024); 
 pthread_attr_t attr;
 pthread_attr_setstack(&attr, p, 2);
 pthread_t thread1;
 pthread_create(&thread1, NULL, foo, NULL);
 pthread_join(thread1, &ret); 
 return 0;

But still I am a lot to learn…

Thoughts on malloc()

* Always check for equal number of malloc()/free() calls.
• Use calloc() instead of malloc() + memset()
• Instead of allocation memory in a piece-meal manner, try to allocate a bigger chunk with malloc() and keep on resizing it with realloc() if needed.
• malloc() over-commits the memory i.e. it gives you an address which actually does not exist!
• malloc() grouping & reallocing with “more than you want”

One interesting question: What is the behavior of following program?

int main()
    int *p = (int*)malloc(10);
    p[10] = 1024;