Random Linux Tips

“, “>”, $contents);
$contents = nl2br($contents);

print “

print $contents;
print “



Posted in General | Tagged , , , | Leave a comment

pin application window to specific monitor in linux

At work, I use an external monitor connected to docking station. While all my work happens on the external 24″ monitor, I wanted my email window on the laptop screen so that I have quick view of email any time I need without switching windows. Usually, this is a one time exercise if you don’t disconnect your laptop from the docking station after coming to work. But in most cases, you will end up undocking and docking several times each day. It was a pain to rearrange the windows each time and here I will explain how I automated it today.

I use Ubuntu 15.04 on Lenovo Thinkpad T450 with a Lenovo docking station and the settings described below are for these. Others might need minor changes.

Detecting the docking station

Earlier docking stations could be detected with a line similar to the following in /etc/udev/rules.d/85-dockingstation.rules

[cci_bash]SUBSYSTEM==”platform”, KERNEL==”dock.0″, ACTION==”change”, RUN+=”/etc/thinkpad/dock.sh”[/cc]

This method didn’t work on my docking station. Then I had to look for something else that could help me identify the docking station.

To get all the udev events happening while the laptop is docked, use the following command.

[cci_bash]sudo udevadm monitor –kernel –property –udev[/cc]

This would print  both kernel uevents as well as udev events with the properties. In my case, it gave me a bunch of events. I just chose this one.

[cc lang=”bash”]



Create the udev rule

In the above output, pick a few parameters which are unique for this to create the udev rule for this event. Create a file /etc/udev/rules.d/85-thinkpad-dock.rules and paste the following into that.

[cci_bash]ACTION==”add”, SUBSYSTEMS==”usb”, ENV{ID_MODEL_ID}==”1012″, ENV{ID_MODEL}==”Lenovo_ThinkPad_Dock”, ENV{ID_VENDOR_ID}==”17ef”, ENV{PRODUCT}==”17ef/1012/5040″ RUN+=”/bin/su jemshad -c ‘/etc/thinkpad/dock.sh'”[/cc]

On the occurrence of the event, whatever command mentioned in RUN will be executed. /bin/su makes sure it is executed as required user than root.

ACTION==”add” – Execute only when the “add” event occurs.

Rest everything is meant for uniquely identifying this event.

Script to detect state of docking

Crate /etc/thinkpad/dock.sh with something similar to

[cc lang=”bash” tab_size=”4″ escaped=”true”]

#!/bin/bash -x

# Log everything in syslog
exec &> >(logger -t ‘[laptop-dock]’)

echo “laptop docking state changed”

DOCKED=$(cat /sys/devices/platform/dock.0/docked)

case “$DOCKED” in

#undocked event
[[ -x /home/jemshad/bin/undocked ]] && timeout 120 /home/jemshad/bin/undocked
#docked event
[[ -x /home/jemshad/bin/docked ]] && timeout 120 /home/jemshad/bin/docked
exit 0


  • The exec line makes sure that I get all the activity logged in the syslog (/var/log/syslog on Ubuntu)
  • timeout makes sure that the script gets terminated after 2 minutes. This is to prevent it from running endlessly in case the display is not available or ‘Google Chrome’ is not opened (in my case).

Script to move the required window

[cc lang=”bash” tab_size=”4″ escaped=”true”]

#!/bin/bash -x

export DISPLAY=:0

# Wait for some time for the external monitor to turn on
while (( num_displays < 2 ))
displays=$(xrandr -q 2>/dev/null | grep -wF ‘connected’)
echo “Displays: $displays”
num_displays=$(wc -l <<<“$displays”)
sleep 2

# Is it really the docking station connected
# Don’t want to project my email to everyone when connected in a meeting
if [[ $displays == *”1366×768+1920+25″* ]]; then
# Wait till Google Chrome is opened
while :
is_chrome_open=$(wmctrl -l -F | grep ‘Google Chrome’)
if [[ -z $is_chrome_open ]]; then
sleep 5
wmctrl -r ‘Google Chrome’ -e ‘0,1920,25,1366,1128’


DISPLAY variable need to be exported for the xrandr to work properly. In my case, I open my work email in ‘Google Chrome’ always. I check for a couple of things in the script here:

  • External monitor takes a while to switch on and therefore I need to wait for it.
  • Make sure that it is the docking station which gave me an external monitor and not just a projector connected to the laptop by checking for the resolution of the monitors.
  • As a next step, wait for Google Chrome to run and using wmctrl, move it to the other monitor (laptop).
    • The values to be given there can be easily obtained by moving the application window manually to the required position first and then running wmctrl -l -G. The output would look something like
      [cc lang=”bash”]
      0x05200007 0 1476 864 444 312 IM0713-L0 Software Updater
      0x01800007 0 0 0 1920 24 IM0713-L0 Top Expanded Edge Panel
      0x02e0000b -1 0 0 3286 1200 IM0713-L0 Desktop
      0x03400027 -1 0 1036 1920 164 IM0713-L0 Docky
      0x04200001 0 1920 25 1366 1128 IM0713-L0 Sign in – Google Accounts – Google Chrome
      0x04e0000b 0 6 52 1908 1094 IM0713-L0 Terminal
      0x0420007b 0 1570 246 350 440 IM0713-L0 Hangouts -xxxx@xxxxx.com
      0x05400001 0 0 24 1920 1128 IM0713-L0 Chapter 7: Forms – Chromium
      0x05400001 0 IM0713-L0 Chapter 7: Forms – Chromium
      Take the values after the first field in order. 0, 1920, 25, 1366, 1128 in this case for Google Chrome and provide them in your script.

That should be it!. Let me know if it helped you 🙂


Posted in linux, tech | Tagged , , | Leave a comment

shrink VirtualBox disk image

For a recent hackathon at InMobi, I brought up a VirtualBox VM using vagrant. It had couple of java applications, nginx and ab hitting the nginx to generate traffic. I left the vm running even after the event and in a couple of day’s time I started getting disk space notifications on my laptop (host machine). The VM disk image (box-disk1.vmdk) file was around 17GB by that time. When I login to the vm and check disk space, it was only around 2.5GB. Obviously, something was wrong. Eventhough log rotation and cleanup was configured on the VM, space once used was not getting cleared on the disk image file even when the files inside the VM were deleted. Though I didn’t want the vm to be running, I didn’t want the vm disk image taking up this much space on my laptop as well.

Steps to Shrink the vmdk disk image

A lot of the posts on internet explains how to extend the image. What I wanted to do was to shrink or compact it without losing the data. As the disk image on host was showing much bigger size than the actual usage on VM, first step is to zero fill all the empty space on the vm. One way to do this is using zerofill command.

zerofill works on the file system and therefore requires the vm to be booted up using a different boot image. Once inside the vm, install zerofill package and run:

$ sudo zerofill /dev/sda1

Assuming /dev/sda1 is your vm root disk.

When you do not have a live image to boot up the vm, this is what you can do. Boot up the vm and inside that, run the following

# dd if=/dev/zero of=file; sync; rm -f file

This would essentially fill up the entire disk image with one file full of zeros and then remove the file.  Shutdown the vm and clone the disk image using VBoxManage command. It will skip all the zeros and what you get is a disk image with actual disk usage of the VM!

$ sudo VBoxManage clonehd box-disk1.vmdk box-disk2.vmdk

Most of the steps mentioned here are from this website. The only difference would be that you don’t really have to convert the vmdk to vdi format and back to achieve this.

As a final step, go to the VirtualBox settings for the VM and change the hard disk to point to the new file and you are good to go.

Posted in linux, tech | Tagged , | Leave a comment

remove wireless device limitation with airtel broadband

Recently, I applied for an airtel broadband connection. Everything went really fast and smooth till the technician came home to setup the modem for broadband. I had opted for the free wi-fi modem from Airtel and the one they provided me was Binatone 850w. Surprisingly, this modem that came with Airtel had a firmware locked to Airtel!. I had two issues with this setup

  1. Max you can connect only 4 wireless devices. If you have two laptops and two phones at home, that itself occupies all the 4 slots. If you have a tab or if your friends or relatives visit your place, they won’t be able to connect their devices to your modem using wifi till you knock out one of the earlier registered devices and then register the new device. This seriously, is a PIA!!!.
  2. The Airtel technician who came for setting up the modem had no clue on how to how to configure Android phones for connecting to WiFi. According to him, the phones would just connect automatically which was absolutely wrong. I was able to connect successfully using the same authentication settings as what my laptop showed (Network Manager in Unbuntu shows the connection information). But the problem with that was that, if I go out of wireless range and then come back again, the phone was not auto connecting to WiFi. In fact, all the steps for configuring Phone is mentioned on the Airtel website itself ( Though the doc is for Samsung, it applies to any android phone. Similar doc is available for Nokia phones also on their website. Technicians are not aware of any of these!

Searching on the internet for an unlocked firmware for this device didn’t give much results. I think the 850w is a new modem replacing the old 845w which Airtel was providing earlier. I sent a mail to Airtel customer care detailing the problem that I am facing and also at the same time, I contacted the guys at Binatone for an unlocked firmware. In the mean time, I searched the entire Airtel website ( for anything that could help me here. I noticed that had  two directories. – 850_hide and 850_No hide. All you have to do is, download the firmware (filename – ras) inside the “No hide” directory and use that firmware with the 850w router. The stock firmware that came with the modem wouldn’t allow you to change the Wireless Authentication – all that would be greyed out (Unchangeable). This firmware inside the No Hide directory would unlock all that and you can configure the modem with WPA2-PSK with AES which is what I did. No more limitation on number of devices and phone is also happy. The Binatone 850w also has mulltiple SSIDs.

Folks at Binatone did give me a firmware, but unfortunately that didn’t work for me. Later I got call from Airtel customer care regarding my query. As usual, the first level guys that you get to talk to initially know nothing about all these. I had to escalate the call to talk to someone from the technical team and they said they can’t increase the limit of only 4 devices. If I can provide them my internet IP, they said they will login from there and change the firmware to unlock the settings which I had already done.

I am also attaching the unlocked firmware here in case Airtel decide to remove that from their website later 🙂 You can get it here – dt850w-a1.006.ras

Airtel seem to be implementing this Enterprise Authentication based on telecom regulations

In that case, they should allow the users to request for increasing the number of wireless devices that can be registered on their portal. In fact, docs on their site says that subscribers can ask for increase – on PAGE 38, says

A master can create up to 3 child accounts
This limit can be changed by the administrators on a per subscriber basis.

Calls/Emails to Airtel asking for increase on this was not really helpful. As per them, they can’t increase this limit of 4 devices. If someone asks for increase, they will provide the unlocked firmware!. Nice way of implementing telecom regulations, isn’t it? 🙂

Hope this helps!


Posted in General | Tagged , , | 79 Comments

gmail ignores dot in the email address

Though dot (.) is a valid character in a gmail id, gmail never really uses that. Instead, it ignores the dot(s) in the email address. This means that, if your email id is foo.bar@gmail.com, you can login to the same account as foobar@gmail.com, foob.ar@gmail.com or any combination like that. Now that is not really an advantage. Nobody would want to login to the same account with multiple ids.

Creating aliases for your gmail account using this feature:

The real advantage is when using the same email account for different use and avoiding clutter by setting up labels based on the To: address of the email. I can, for example, give foo.bar@gmail.com to my friends and foobar@gmail.com to others. After that, all I have to do is, setup filters so that all mail for foo.bar@gmail.com get labeled with ‘Friends’ and sorting becomes easy. You don’t need multiple email accounts. The position of the dot can be changed or more dots included to create more and more aliases for your email address. The fact that gmail ignores dot in the email address was told to me by Razee Marikar.

Posted in linux, tech | Tagged , , | 3 Comments

smart wc(1)

As well all know, the -c options of wc(1) command in linux gives the number of bytes in a file. wc -c never reads the entire file for counting the number of bytes. Instead, it does a stat on the file and gets the number of bytes occupied by the file. This is the case if we say wc -c file. Now consider the case of wc -c < file. That also gives the same output – the number of bytes occupied by the file. In this case, the difference is that, the redirection is setup by the shell and the contents of the file is fed to the stdin of wc. Some smart implementations of wc will figure out the actual filename by doing a stat on the stdin and gets the byte count by doing stat on the file. This is highly efficient compared to reading the entire file and counting the number of bytes. wc on latest linux distributions does this way. The same on FreeBSD or solaris will take longer time to execute the same command.

[cc lang=”bash”]
$ ls -lh somebigfile
-rw-r–r– 1 jemshad jemshad 472M Apr  1  2009 somebigfile

$ time wc -c somebigfile
494786425 somebigfile

real    0m0.003s
user    0m0.000s
sys    0m0.004s

$ time wc -c < somebigfile 494786425 real    0m0.003s user    0m0.000s sys    0m0.004s [/cc] Source: #bash channel at irc.freenode.net

Posted in linux, tech | Tagged , , | Leave a comment

Ergonomic keyboard to help with RSI

Normal vs Ergonomic k/b

Repetitive Strain Injury (RSI) also known as Cumulative Trauma Disorder (CTD) is one of the common problems among software professionals. For me, it appeared as a pain in the wrist joints, shoulder pain and neck stiffness. The pain in the wrist joints where much serious than rest of them. The adjustable keyboard tray with mouse pad and fully adjustable chair allows me to sit properly and work. But the normal keyboard requires bending of wrist for typing. This started getting me trouble. The picture shows the difference.

The Keyboard:

Then I got myself a Microsoft Natural Ergonomic Keyboard 4000. It alligns perfectly with the normal position of arms and therefore eases the typing effort. No more bending of wrists and the comfortable pad gives enough support. It has lot of shortcut keys which you can configure in windows. I always work on linux and therefore kind of useless for me. Correcting the posture or properly keeping the arms won’t fully avoid RSI. The best method is to take short breaks in between and do small bending and stretching excercise. That works the best. Next section explains how I force myself to take breaks in between. It is not an easy task which you can do just by determination. Sitting in front of the computer, you won’t realize how fast time goes.

Take short breaks:

Yes, I know – it is easier said than done. Everyone knows that you should take breaks if you are typing for a long time or doing something continuously for longer duration. But it seldom works. That is when I accidentally happened to see this software rsibreak in ubuntu repository. It is a KDE4 application which will lock the screen at configurable intervals. You can configure the time intervals as well as the duration. While the screen is locked, it shows a countdown timer also. Just utilize that time to give some excercise for your body parts. I had always been a KDE user. But KDE4 failed on me. It was fancy, it was colorful and all that, but not something I wanted. So I moved to GNOME. The only thing I missed in gnome was KDE’s klipper which is far better than gnome’s glipper. Moving to gnome meant finding an alternative for rsibreak also. After upgrading to Ubuntu 9.04, I noticed that there was an inbuilt mechanism to do something similar. Now as I am typing this, it locked my screen once. We can postpone the break using a small button at the bottom right of the screen, but it will popup again after some time, forcing the user to take a break. Trust me, a software like this helps a lot. Though we all know that short breaks help in avoiding RSI and other computer related health problems, we never do it. We never get time to do it; thats the fact.

Enable Typing breaks in Ubuntu 9.04:

Go to System -> Preferences -> Keyboard. In that, select the tab “Typing Break” and enable the options. Configure the timings according to your preference and you are set. Try it out and let me know if it helped you 🙂

Posted in General | Tagged , , | 1 Comment

Kerala Ayurveda Health Spa – Indiranagar

Phone: 080 25255152   |   9343463923
Address: 794, 9th A Main Road, Indira Nagar 1st Stage, Bangalore- 560038
Landmark: Near Coffee Day (CMH Road)

I had been to this place in May 2009 on a saturday morning for a whole body massage as I had started feeling shoulder pain and neck stiffness, which are probably after effects of the job am into. Sitting in front of the computer and working had started getting me RSI on my wrists which I overcame by a few steps. Searching on net for kerala ayurvedic massage centers gives lot of results, but with almost no reviews. Kottakkal arya vaidya shala would have been great, but their only branch in Bangalore is at Malleshwaram, which is too far from Indiranagar. This place was somewhat near to me and I thought I would give it a shot.

It was a small dark room with a dark wooden massage table in the center. You have to change to a single piece of cloth (which they provide) before they start the massage. Warm oil is poured in drops all over they body and two people standing on either sides of the massage table will do it synchronously. I liked their synchronization – it was almost perfect. They didn’t ask me if it was too rough or soft for me. They were just doing it at their will. The whole thing was for one hour split into two sessions. Massage for 45 minutes and 15 mins, hot water bath. They provide towel and soap which I had taken with me in case I needed. Though the massage, the ambience and all was good, after coming out of it, I didn’t really feel refreshed – something I was expecting out of a whole body massage.

Posted in General | Tagged , , , , , | 4 Comments