Wednesday, February 24, 2010

Install PostgreSQL8.4 in Karmic

From terminal type
$sudo apt-get install postgresql
answer yes to all questions.
You may also want to install pgAdmin III. It good tool to manage PostgreSQL especially if you are a beginner. In terminal type
$sudo apt-get install pgadmin3
again, just answer yes to the questions.
To begin using postgre, you need to set postgre user password.
$sudo -u postgres psql postgres
enter your password.
psql (8.4.2)
Type "help" for help.

postgres=# \password postgres
Enter new password:
Enter it again:
postgres=# CREATE DATABASE database_name;
postgres=# \quit
done. Refer to for complete tutorial.

Display Services Running on Linux and Their Port

In linux, you can easily check which services are running and their corresponding port. Use command
$ cat /etc/services
Pipe it to grep to get particular program, instead of browsing the results. For example, to search which port Postgre running you use
$ cat /etc/services | grep postgre

postgresql  5432/tcp   postgres   # PostgreSQL Database
postgresql  5432/udp   postgres
You can see that postgresql is listening to port 5432. Of course there are many other ways around. This is just one of them.

Dbase Driver For Java

On our last project, we migrated legacy application, which based on FoxPro Dos to Java and Postgre database. For some reasons, we could not migrate all at once.  Instead, we wrote java app first to connect to legacy database (dbf). Then later, change the database to Postgre.

After days of search we came accross to this driver by Hongxin Technology & Trade Ltd. of Xiangtan City. It was not free though but it works.

Connecting to database was easy. It just like any other jdbc drivers out there. It supports subdirectory reading, idx, ndx, etc. In addition, the driver allows you to set as server mode, poolable connection on many application servers. For complete features please refer to faq.

Although it mainly works, we have a bit complain. It's slow. Maybe the problem lies on underlaying database file, i am not sure about this. Lack of documentations prevent us to do driver optimization.

Overall, it is a good product.

Monitoring File Changes on Linux 2

Referring to my previous post Monitoring File Changes on Linux, there is other alternative to do it

#! /bin/sh

# This is the log that will be monitored. 
# If any changes occur to this, then take appropriate action.

# We will need this log to see whether any changes occured to /tmp/goreb.log

# This is a 1-time command i.e. create the log file if it does nto exist.
[ ! -f $TEMP_LOG ] && touch -r $MONITORED_LOG $TEMP_LOG

[ $MONITORED_LOG -nt $TEMP_LOG ] && echo "$MONITORED_LOG is newer than $TEMP_LOG. Carry out your required operations"

# Update $TEMP_LOG with the new modified date of $MONITORED_LOG

Monitoring File Changes on Linux

This is a copy of article from (Copyright Anthony Lawrence, used by permission) In case the original link dead, I can still look at it.

Many modern systems provide a way to watch a directory for events (new files, reading the directory, modification of a file in the directory, etc.). This facility can be done in various ways, from providing hooks in the filesystem code itself to something that watches for inode changes. Linux and BSD have several possibilities in that regard, including dnotify ( replaced by inotify), changedfiles, watch, Gamin and fam.

Mac OS X has FileMonitor (shareware), FSEvents and it's certainly possible that some of the BSD tools might compile on OS X. But what do you do when there is no support in the OS?

Brute Force

Typically you are interested in new files in a particular directory. You can do something like this:
touch /tmp/testdirb.$$
while true
ls /testdir > /tmp/testdira.$$
diff /tmp/testdira.$$ /tmp/testdirb.$$ || echo "changed"
cp /tmp/testdira.$$ /tmp/testdirb.$$
sleep 300
If you were looking for a particular file to change, you'd use "ls -l", and if your interest was in if the file was being used or executed, "ls -lut" would give you that. This example justs echoes when something changes, but you would more likely call some other script that did more testing. One obvious issue that comes up if a file has been created is waiting for the creating program to have finished up: lsof or fuser can help you with that.

But this is all pretty crude. Sometimes crude is fine, but if you need to know more often, there's a fair amount of overhead in this that you really don't need.

If it is just one file, and your interest is additions to it, the mailchecking utility in your shell can give you alerts. For example, in Linux bash:
You can watch multiple files, each with its own message, by using MAILPATH instead of MAIL. You need to "unset MAIL", and set and export MAILPATH:
unset MAIL
MAILPATH='/tmp/foo/h?"H is changed":/tmp/foo/a?"A has changed"'
export MAILPATH 

Linux systems have a command line "stat" that can make checking changes a little less intensive. "stat -t testdir" looks something like this:
testdir 4096 8 41ed 5001 5001 303 82022 2 2b 1d 1070127877 1070128608 1070128608
which is everything you want to know about the file or directory for this purpose. Security Enhanced Linux versions add another field which we need to strip out for our purposes here:
OLD=`stat -t testdir`
# OLD=`stat -t testdir | sed 's/[0-9][0-9]*$//'`  # if SE linux stat
while true
NEW=`stat -t testdir`
# NEW=`stat -t testdir | sed 's/[0-9][0-9]*$//'`  if SE linux stat
[ "$NEW" == "$OLD" ] || echo "changed!"
sleep 3
You could get fancier by splitting out the fields into separate variables. That isn't a lot of fun at the shell level, so we'll move up a notch.

Perl or C

From Perl or C (and of course many other languages), you can get access to the stat information a bit more easily. Here's a simple Perl example like those already given:
@info=stat("testdir") or die "Can't stat testdir $!";
while (1) {
@newinfo=stat("testdir") or die "Can't stat testdir $!";
@what=qw(Device Inum Mode Links Owner Group Rdev Size Atime Mtime Ctime PBlock Blocks);
while ($info[$x]) {
  system("/bin/echo $what[$x] $info[$x] $newinfo[$x]") if ($info[$x] ne $newinfo[$x]);
sleep 1;