Saturday, August 23, 2008

Eclipse Shirt Is Here!

FedEx dropped off the much anticipated Eclipse t-shirt today and I must say, it looks fab! I got it as a result of taking part in the Ganymede Around the World Contest. My coder friends are going to be so jealous!

I've posted a few pictures of the t-shirt here. Enjoy!


Front
Back
Eclipse Logo
Eclipse
Sleeve

Related post:
Here is my review of Ganymede, for those who missed it: Eclipse Ganymede Review

Monday, August 18, 2008

Subtract Two Dates [Unix]

In this post, I will describe how you can subtract two dates (which are in the form yyyyMMdd) to get the time between them. My approach involves first parsing both dates in order to extract the year, month and day, converting them to seconds using floating point math and finally subtracting one from the other.

Bash doesn't support floating point arithmetic, but I won't let that stop me. I will use awk instead. Another possible candidate is bc.

1. Parsing the date
We need to extract the year, month and day from our date which is in yyyyMMdd e.g. 20080818. The year is composed of the first four characters, the month is the next two and the day, the final two. We can do this using awk's substr function as shown below. substr(s,m,n) returns the n-character substring of s that begins at position m.

today=20080818
echo $today | awk '{
 year=substr($1,1,4);
 month=substr($1,5,2);
 day=substr($1,7,2);
 }'
If you don't like awk, you can also get substrings using the shell's expr command:
today=20080818
year=`/usr/ucb/expr substr $today 1 4`
month=`/usr/ucb/expr substr $today 5 2`
day=`/usr/ucb/expr substr $today 7 2`
2. Converting to seconds
We will use the formula below to give us the number of days since 1/1/1970:
(year-1970)*365.25 + month*30.5 + day

Now convert the days to seconds by multiplying the days by 24 * 60 * 60:
((year-1970)*365.25 + month*30.5 + day) * 24 * 60 * 60

3. Subtracting the times
Once we have both dates in seconds, we can subtract them. Since we're still dealing with floating point we should use awk or bc for precision.
echo $seconds1 $seconds2 | awk '{print $1 - $2}'
or
echo $seconds1 - $seconds2 | bc
Putting it all together
Here is the complete shell script which takes two dates and returns the number of seconds between them:
#!/usr/bin/bash

date1=$1
date2=$2

#give the dates to awk
echo $date1 $date2 | awk '{

#parse
year1=substr($1,1,4);
month1=substr($1,5,2);
day1=substr($1,7,2);

year2=substr($2,1,4);
month2=substr($2,5,2);
day2=substr($2,7,2);

#get seconds
secs1=((year1 - 1970)*365.25+(month1*30.5)+day1)*24*60*60;
secs2=((year2 - 1970)*365.25+(month2*30.5)+day2)*24*60*60;

#subtract
print secs1 - secs2;
}'
As you can see, all of the computation is in awk! So we could put all of our awk code into a separate file called subtractDates.awk, for instance, and run it on the command line like this: echo 20080830 20080818 | awk -f subtractDates.awk

Now that we have the number of seconds between two dates, we can convert that to days, months and years using a similar approach if we have to.

Sunday, August 17, 2008

fahdshariff on Twitter

I think it's time to give Twitter a try and see what all the fuss is about. You can follow me if you like (though I don't think it will be very interesting or anything): fahdshariff on Twitter.

However, if I find that it's useless or that I'm wasting too much time with it, I'm outta there! I'll report back and let you know what I think of it.

Do you love or hate Twitter? Any tips? Share them in the comments!

Friday, August 15, 2008

Check if Process is Running [Unix]

If you have a process id (pid), how can you tell if it is still running?

One obvious way to do this is to parse the output of the ps command. For example:

sharfah@starship:~> ps -ef | grep " 1234 " | grep -v grep
sharfah  1234     1  0 10:36:06 pts/65   1:20 java -server
However, a much neater way to do this is to use the kill command to send signal 0 to the process. So kill -0 will not terminate the process and the return status can be used to determine if the process is running. 0 if it is, non-zero otherwise.
sharfah@starship:~> kill -0 1234
sharfah@starship:~> echo $?
0

sharfah@starship:~> kill -0 1234x
bash: kill: 1234x: no such pid
sharfah@starship:~> echo $?
1
For those interested, the kill -l command lists signal numbers and names, but 0 is not listed.
sharfah@starship:~> kill -l
 1) SIGHUP     2) SIGINT      3) SIGQUIT   4) SIGILL
 5) SIGTRAP    6) SIGABRT     7) SIGEMT    8) SIGFPE
 9) SIGKILL   10) SIGBUS     11) SIGSEGV  12) SIGSYS
13) SIGPIPE   14) SIGALRM    15) SIGTERM  16) SIGUSR1
17) SIGUSR2   18) SIGCHLD    19) SIGPWR   20) SIGWINCH
21) SIGURG    22) SIGIO      23) SIGSTOP  24) SIGTSTP
25) SIGCONT   26) SIGTTIN    27) SIGTTOU  28) SIGVTALRM
29) SIGPROF   30) SIGXCPU    31) SIGXFSZ  32) SIGWAITING
33) SIGLWP    34) SIGFREEZE  35) SIGTHAW  36) SIGCANCEL
37) SIGLOST

Tuesday, August 12, 2008

Organise RSS Feeds in Google Reader

My list of RSS feeds is constantly growing. Everyday, I discover new and interesting sites and blogs which I subscribe to. They vary in content and I read them based on how important they are, their content and what mood I am in. At the time of writing this post, I have nearly 70 feeds in Google Reader, distributed haphazardly across three default folders: Fun, Geeky and News. There is also a bunch lying in the root folder.

It's time to clean up and get organised as I am spending too much time sifting through junk which I don't want to read right now. I need to get rid of obsolete feeds and create a better folder structure for those I want to keep. Using Google Reader to create folders and drag my feeds in is going to take forever and probably a thousand mouse clicks.

I am going to try an easier way. I will export my feeds from Google Reader into an OPML file and then edit that using my favourite text editor (TextPad) in order to create the structure I want.

This is how:

  1. Go to your Google Reader Settings page and click on the Import/Export tab
  2. Click on the Export your subscriptions as an OPML file link and save the google-reader-subscriptions.xml file to your computer
  3. Open the xml file in TextPad (or any another text editor) and the format will look pretty obvious.
  4. After reorganising your feeds within the OPML file, go back to your Google Reader Import page and Upload the modified OPML file
The OPML file contains nested outline elements which represent folders containing RSS feeds.

Here is an excerpt from my newly formatted OPML file, which shows different folders (Fun, Programming, Low Priority) and the feeds within:
Do you have a better way to organise your RSS? Share it in the comments!

Wednesday, August 06, 2008

The fuser Command

fuser is one of the *nix commands that I have started seeing myself use more and more frequently. It is a very powerful command, yet often forgotten. So what is it? fuser displays the process IDs of the processes that are using the files specified.

Identifying Processes
The following example shows how you can use the fuser command to find out which process is writing a log file called log.txt. fuser tells you the process ID (24976), which you can then use in a ps command to find out what the process is. In this case, its java.

sharfah@starship:~> ls -ltr
-rw-rw-r--   1 sharfah   sharfah     2836 Aug  6 00:08 log.txt

sharfah@starship:~> fuser -u log.txt
log.txt:    24976o(sharfah)

sharfah@starship:~> ps -ef | grep 24976
  sharfah 24976 24963  0 23:49:00 ?       13:39 java -server
  sharfah  6284 17191  0 00:09:54 pts/1    0:00 grep 24976
Used in this way, fuser can help tell you which process is responsible for creating large log files on your filesystem!

Killing Processes
The -k flag, sends the SIGKILL signal to each process using the file. This is handy if you want to "kill -9" a process without hunting for its PID first. Alternatively, if you want to send another signal type, use the -s flag. e.g. to send SIGTERM use -s TERM. The -k option is equivalent to -s KILL or -s 9. Most of my shutdown scripts, simply call fuser -s TERM on the process's log file.

Because fuser works with a snapshot of the system image, it may miss processes that begin using a file while fuser is running. Also, processes reported as using a file may have stopped using it while fuser was running. These factors should discourage the use of the -k option.

Friday, August 01, 2008

Eclipse Shirt Is Coming!

I finally received the following email from Lynn Gayowski, of the Eclipse Foundation, about the shirt they promised for taking part in the Ganymede Around the World Contest:
Thank you for submitting a review of the Eclipse release to the Ganymede Around the World contest. We have an Eclipse shirt for you! Please send me your:
1) Name
2) Shirt size (men’s small, medium, large, X-large, 2X-large or women’s small, medium, large)
3) Mailing Address
4) Phone Number (Fedex requires this for deliveries)

The contest closed yesterday, so all blog entries have been passed on to a panel of judges and we hope to have the winners of the Eclipse jackets and conference pass announced in a few weeks.

Thank you for participating and best of luck in the contest.

Regards,
Lynn Gayowski
Marketing Events Manager
Eclipse Foundation, Inc.
P (613) 224-9461 ext. 234
F (613) 224-5172
lynn.gayowski@eclipse.org
www.eclipse.org

I have sent them my details and am looking forward to getting my shirt. I will be posting pictures here as soon as it arrives, so come back soon! Better still, subscribe to my feed!

Related post:
Here is my review of Ganymede, for those who missed it: Eclipse Ganymede Review