Tuesday, 8 January 2013

Kill all zombie processes of a process

Time and again I have found phpmyadmin not working because there are a lot of zombies of httpd. I do not know yet why these many instances of the daemon show up and why they turn into unresponsive zombie processes. Usually when this happens, I just kill all the zombies and spawn a new daemon and I carry on with my work. I used to kill all the zombies one at a time. However, today I figured out that I can do it with a single line.

ps aux | grep http | awk -F " " '{print $2}' | xargs kill -9

[If the working is clear to you, do read further and let me know if I can improve it or if I am interpreting anything incorrectly although things are somehow working.]

The one-liner above is easy to understand once we have understand the pieces. So, I am listing that below.


  • ps aux lists all running processes
  • grep http finds the lines containing the string 'http'
  • awk -F " " '{print $2}' splits each input line by delimiter specified with -F flag, space in this case and prints the second token thus obtained
  • kill -9 send SIGKILL signal to the processes whose ids are specified as arguments
  • xargs takes output of previous command and makes it input for the next
You should probably have a look at the man pages for more details on the commands.

Wednesday, 26 December 2012

Rails is re-inventing the wheel

In recent years, there has been a surge in web applications. To support the growing market, frameworks have developed around scripting languages for developing web applications fast. Of those Ruby on Rails seems to be the most mature. Django, the most advanced Python framework for web applications is yet to come at par with it.

Be it Django or Ruby on Rails, both follow a model-view-controller architecture to web applications. Now, many web applications using these frameworks follow 'skinny controllers, fat models'. As a result the models become home to a lot of business logic. Models are intended to serve only as an abstraction to the database. They are meant to be 'models of data'. Also, when the application grows huge, we run into issues of scalability and we resort to techniques like sharding. Now, there are of course multiple ways of sharding and you need to decide what suits you better. Here, I list a few ways for Rails applications.
Now, sometimes the sharding logic also creeps into the models. Obviously objects of model classes do not have a single purpose any more. Over time, the model classes obviously become too complicated. It becomes difficult to maintain and debug. The Rails community is becoming increasingly aware of these issues. There are multiple ways in which developers are trying to move business logic away from models. Here I list of couple of those ways.
Looking at the overall scenario reminds me of building N-tier applications using Java Enterprise Edition. The sharding logic and details of data fetching can be a tier below models and the business logic can be a tier above it. ' seems to me developers using web frameworks like Django and Rails are just re-inventing the wheel. Also, Java has optimized garbage collection which can be tuned to various needs. In Ruby garbage collection seems to be a big issue, though good work in the area is coming in the next version.

So much in the name of innovation, eh? In case my ideas seem to waver from reality do point me in the right direction.

Update: You can follow the discussion on Hacker News on the topic.

Monday, 24 December 2012

Rails time formats

During programming, I do tend to forget the various time formats available in Rails. Also, there are so many options available that a simple typo like capitalizing (or not) changes meaning. It is best that this part be always tested properly.

Sunday, 16 December 2012

Apple Magic Mouse with Dell Latitude E6520

Recently, I found some free time at office and my colleague's Apple magic mouse was lying on my table. It sure is a cool device. So, I thought why not connect it to linux and see how it works. So, I turned on the bluetooth daemon on my system with the following command.

systemctl start bluetooth

I use Bluedevil. So, I clicked on the tray icon and ran a scan for bluetooth devices in range. The pairing key required is 0000. Initially the mouse pointer movement was too fast and scroll was too slow. So, I changed the configuration using this blog.

It is cool, indeed. However, I found it difficult for regular use because I am not used to it and it is a touch mouse unlike other mice. When I am moving the mouse, I have to make sure my index finger is not touching the surface, else there is unwanted scrolling.

Friday, 14 December 2012

Enjoying writing

Recently while waiting for a flight at Mumbai domestic airport, I decided to write down my thoughts. Of late, I have been writing on some keyboard mostly but this time I was writing with a pen and I actually enjoyed doing it. So, I write  about writing only. This is what I wrote:

Is it possible to write with this pen?
Well not completely impossible. Also I realize the benefit of writing with a thick pen. With a thick pen, you tend to write each letter well while with a thin pen, you write a letter away [especially the letters towards the end of word]. [It] Reminds me of a friend who recently advised me to try a thick pen and I have to admit I am really loving this experience. At school, I hated writing with a thick pen because it slowed me down. However, now that its not my writing speed by my writing that I care about, I think I will use this thick pen more often.


Compared typing on a keyboard, while penning down the flow of thoughts was better. I liked it and decided to do it more often.

Friday, 16 November 2012

UTF-8 encoded Rails Console in linux

Although, initially while working on Rails project, we do not pay much attention to the character encoding, soon it bites us bad. When we try to play with a string containing Unicode characters in an ASCII encoded Rails console, it complains about it and we are stuck.

It is quite easy to get a UTF-8 encoded Rails console. All we need to do is properly set the LANG environment variable of the terminal first.

export LANG=en_US.UTF-8

Rails picks up this variable and sets its encoding. So, when we fire up Rails console now and enter the following command, it shows that we have UTF-8 encoded console.

puts __ENCODING__

Now, we can easily play with UTF-8 strings.

Sunday, 4 November 2012

Skype uses its own DNS

Skype is a really interesting software. Its text chat UI is mature and audio and video chat facility are very nice. Skype has this interesting ability of working independent of the DNS configuration of your system. Even when the DNS server your system is configured for, gets poisoned, Skype does not get affected.

In fact, I leverage this ability of Skype to detect network issues due to DNS service being down. On such an occasion, when the browsers were not working but Skype was, I pointed my DNS configuration to Google's DNS servers (listed below) and the browsers worked fine.

8.8.8.8
8.8.4.4

To take a deeper look, I tracked the connections Skype was making. I used the following to do that.

netstat -taucp | grep skype

Initially it was sending out packets from port 51395 of my localhost. After I logged in, I find the following connections:


tcp        0      0 *:42520                 *:*                     LISTEN      19207/skype      
tcp        0      1 H:47078          111.221.77.159:40016    SYN_SENT    19207/skype      
udp        0      0 *:42520                 *:*                                 19207/skype      
udp        0      0 localhost:51395 *:*                                 19207/skype

H about is the hostname of my system and the IP 111.221.77.159 is owned by Microsoft, Microsoft Singapore to be specific. Interesting sequence of connections, when I lookup a contact and open the corresponding chat window.


tcp        0      0 *:42520                 *:*                     LISTEN      19207/skype      
tcp        0    516 H:46808          193.95.154.39:33033     ESTABLISHED 19207/skype      
tcp        0      0 H:47078          111.221.77.159:40016    ESTABLISHED 19207/skype      
udp        0      0 *:33626                 *:*                                 19207/skype      
udp        0      0 *:41980                 *:*                                 19207/skype      
udp        0      0 *:42520                 *:*                                 19207/skype      
udp        0      0 localhost:51395 *:*                                 19207/skype      
udp        0      0 H:32897          *:*                                 19207/skype

The IP 193.95.154.39 maps to Skype Technologies. After sending a line of text on  the chat window, the following connections were made.


tcp        0      0 *:42520                 *:*                     LISTEN      19207/skype      
tcp        0      0 H:46808          193.95.154.39:33033     ESTABLISHED 19207/skype      
tcp        0      0 H:47078          111.221.77.159:40016    ESTABLISHED 19207/skype      
tcp        0    236 H:44172          157.56.123.82:https     ESTABLISHED 19207/skype      
tcp        0      1 H:45020          91.190.216.9:www-http   SYN_SENT    19207/skype      
tcp        0     75 H:48246          78.141.179.14:12350     ESTABLISHED 19207/skype      
udp        0      0 *:33626                 *:*                                 19207/skype      
udp        0      0 *:42520                 *:*                                 19207/skype      
udp        0      0 localhost:51395 *:*                                 19207/skype      
udp        0      0 H:32897          *:*                                 19207/skype

The IPs 157.56.123.82, 91.190.216.9, 78.141.179.14 map to Microsoft Corp Redmond, Skype and Entreprise des Postes et Telecommunications respectively. After that, when I was not doing any more activity on Skype, the following network activity was seen.


tcp        0      0 *:42520                 *:*                     LISTEN      19207/skype      
tcp        0      0 H:47078          111.221.77.159:40016    ESTABLISHED 19207/skype      
tcp        0      0 H:47362          db3msgr6011307.ga:https ESTABLISHED 19207/skype      
tcp        0      0 H:48246          78.141.179.14:12350     ESTABLISHED 19207/skype      
udp        0      0 *:42520                 *:*                                 19207/skype      
udp        0      0 localhost.localdo:51395 *:*                                 19207/skype

db3msgr6011307.ga does not have any DNS entries. Suggestions for digging further are welcome.

Saturday, 6 October 2012

Formatting column entries for IN query

Extracting entries of a column in a spreadsheet and using them in an IN query is not a rare scenario [at least for me] at work. Now, copying and pasting the entries in a comma separated and sometimes quote-enclosed format is tedious and when there are 100 entries, doing that by hand is unacceptable for me. On top of that when each entry has to be modified a bit before passing in the IN query, then the whole process becomes drudgery. Today, when faced with such a situation, I used emacs and sed to save myself a lot of time and boredom.

Let us consider that the spreadsheet has a column that has the following entries:

12345-1
23456-2
34567-1
45678-1
56789-1

Now, lets consider that I have to strip the first suffix, enclose the values in quotes and pass them in the IN query. So, I copy the column and paste in a text file and run the following commands on the file.


sed -i 's/-1//' ./myfile.txt
sed -i 's/-2//' ./myfile.txt

Now the file had the following contents.

12345
23456
34567
45678
56789

Now, I opened the file in emacs and ran a keyboard macro to get the following string.

'12345', '23456', '34567', '45678', '56789',

Now, I could easily use it in my query. Now, the macro I used was simple. I started recording the macro using `C-x ('. The macro consisted of the following keys in sequence [try it yourself to be clear]:

' -> End -> ' -> , -> Delete -> Space

I ended recording the macro using `C-x )'. Now, I had one line edited so I let emacs run the macro 4 times using the following command:

C-u 4 C-x e

I got the inspiration for using macros in the following video.


Thursday, 13 September 2012

JVM crash: an eye opener

I had done some programming but I guess I had not hit many walls. In C, my programs had segfaulted; but every time that happened I knew that my program broke; but there was never a notion of the programming environment breaking down. I, of course, know that when one is coding specifically to break the environment, it is not impossible; but I am talking about cases when the break is not intended. I could never imagine the shell, in which I am running a program for connecting to some website and getting some data, breaking down in between the running of the program.

However, recently I was using Rubymine to test some XML parsers I had written and suddenly it crashed. Initially, I thought may be Rubymine crashed, so I went back to shell where I had started it. To my utter surprise, it was the JVM that had crashed. That is when it hit me that programming environments can also crash. I have reported the bug; but there is not much done from Oracle's side about it.

Well, I do not expect much from them either; but the lesson I learnt is valuable: it is not only the program; but also the program's environment that can crash. Its not that I did not know it but it is not something I regularly expected. From now on, I will though. It did change the way I looked at errors. I guess this is what counts as experience.