Friday, September 5, 2008

AJAX Internals

You can find wrapper class built for AJAX object and effectively used in the following url.

http://www.ajaxtoolbox.com/

Correlate that, AJAX can be studied regardless wherever it is applied.

AJAX stands for Asynchronous Javascript for XML. AJAX provisions another way of making web request (HTTP Get/Post) to the web server (HTTP Server). It is basically a client side object. As part of normal/event javascript calls, we can make this AJAX calls to web server; the advantage we get is the page won't get refreshed unless we render some content thru' client side scripts. Some AJAX frameworks, for example in Atlas.net, the AJAX calls been tided with the server side(.net) event routines, so rendering will look like, been done from server end, but it actually not because always rendering done thru' client side script. AJAX object serves the ready state about the response, so we can say loading till we get the content from the web server. This is general idea about AJAX.

To dive little deeper....

we can create the AJAX object using the following piece of javascript code. This object creation is browser dependent.

To make the web request (GET/POST) the following function can be used. Content from server can be get from the property 'responseText'. It can be normal Text or XML or JSON text. JSON stands for Javascript Object Notation. It is the way to represent Javascript Object in a single string (refer http://json.org/).

xmlHttp.open('GET'/*GET or POST*/
,'http://intranet.hcleai.com/getacllist.jsp'/*url*/
,true/*asynchronous or synchronous*/
,'jacob'/*Username*/
,'secretpass'/*password*/
)

If the method is GET is used, then querystring has to be sent using send function or else should be placed along with url as part of open function.

xmlHttp.send(<>)
/*for examle*/xmlHttp.send('?firstname=jacob&password=teetee')

If the method is POST is been used, then before calling the open function, the request header should be set to be form-urlencoded..

xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');

Result of the web request can retrieved using the following property

var ouput=xmlHttp.responseText;

The status of the web request made can be monitored by the following event call.

xmlHttp.onreadystatechange=function()
{
.....
.....
}

The status of the web request made can be found by the following property

if(xmlHttp.readyState==0) //The request is not initialized
if(xmlHttp.readyState==1) //The request has been set up
if(xmlHttp.readyState==2) //The request has been sent
if(xmlHttp.readyState==3) //The request is in process
if(xmlHttp.readyState==4)
//The request is complete

--
Anand
anand.sadasivam@googlemail.com

Monday, September 1, 2008

Linux - Acquiring super user environment/commands

To run the super user commands in linux you should login as root. Or else either you can use 'su' command to possess the superuser (root) power or by using 'sudo' command we can run the super user command provided the sudo access is granted by the superuser (root).

Let me take you thru' su command of linux. If you are normal linux user you will be provided with $ sign prompt and you won't be given a permission to run superuser (root) commands.

$ reboot
reboot: must be superuser.
$

If you are a user and you want to have super user power, you can use 'su' command to acquire the root user environment.

$su #Acquires root user environment
Password:
#reboot #restart the linux system.

This command also used to acquire other users environment. For example the apache server runs as 'jacob' user process and you want to have control over that. You can use the command su following the username .

$su jacob #Acquires jacob user environment
Password:
$apachectl restart #restart the apache server

If you are the superuser (root), then to acquire a user environment you can just use the su following the username, no need key in the password.

#su jacob #Acquires jacob user environment
$apachectl restart #restart the apache server

If sudo is configured for the for the user jacob. He can run the super user commands as shown below:

$sudo reboot #restart the linux system.

--
Anand
anand.sadasivam@googlemail.com

Linux - Cron Scheduler Configuration

cron is the task scheduler in Linux. It basically runs the given command on a specific time/interval. Any linux user can schedule the task for this cron daemon. cron is a daemon, because it runs as part of all other system services. If this service doesn't run in background, scheduling will not workout. To run the cron daemon as a system service, you have to open Services UI from System Administration Menu from GNOME X-Window UI, and then choose this cron daemon to run as a service. Or else you can use the ntsysv command, and then select cron to as system service. You may required to restart the system once.

#ntsysv #UI will open, there you can select services to run when the system starts

To schedule the command, you have use the crontab -e. It will open a vi editor, there you can see the already scheduled tasks, if nothing got scheduled before, an empty vi editor will open. Following is the format by which the task can be scheduled.

$crontab -e
30 17 1 * * ~/backup/backupscript
~
~
~

:wq #Save and Quit

There are five asterisks (*) separated by any whitespace, and then the task to run.

In place of 1st Asterisk - Minutes 0 - 59 can be given
In place of 2nd Asterisk - Hour 0 - 23 can be given
In place of 3rd Asterisk - Date 1 - 31 can be given
In place of 4th Asterisk - Month 1 - 12 can be given
In place of 5th Asterisk - Day of the week 0 - 6 can be given

So as per the configuration backup script will run by evening 5:30 on first of every Month.

If you are the superuser and you want to configure the cron job for user say 'jacob'. You can use the -u option.

#crontab -e -u jacob #Edit cron con configuration of the user jacob

With -l option you can get the list of task scheduled.

$crontab -l
30 23 1 * * ~/backup/backupscript

--
Anand
anand.sadasivam@googlemail.com

Linux - A Simple Backup Script

Taking backup periodically is one of the common task in Linux. Here let me get you thru' simple bash backup script.

Scenario: Under the user's home directory, 'repos' directory has all the repository underwhich the regular development goes on. As a repository might crash anytime, it is required to take back periodically datewise.

Assumption: Consider both 'backup' and 'repos' folder exists under the user's HOME directory

#------------backupscript--------------

mkdir ~/backup/`date +%d-%m-%Y`
for i in `ls ~/repos`
do
cp -r ~/repos/`echo $i` ~/backup/`date +%d-%m-%Y`/`echo $i`
#recursively copies the folder
done

#------------backupscript--------------

~ => represents the HOME directory of the user.
date +%d-%m-%Y => this command will create current system date in the format dd-mm-yyyy
`` => this takes the command and places the output of the command in place instead.

Step1: This script creates the current dated folder under the 'backup' folder exists in the user's HOME directory.
Step2: Takes all the directory and filenames exists under 'repos' folder exists in the user's HOME directory in the iteration variable i.
Step3: For each directory (or) file, the loop gets iterated.
Step4: Recursively copies the directory/file been took, under the current dated folder comes under backup folder from user's HOME directory.
Step5: Once all the directories/files copied, it's comes out of the loop. That's end of the script.

Save this script and give the executable permission to script. Now your backup script is ready.

$chmod u+x

Now it is the question how to schedule this script to run periodically. Let me get you through about cron scheduler in next blog.

--
Anand
anand.sadasivam@googlemail.com

Linux Commands - Basics

Linux has comparatively many commands than any other operating system in the world. It is difficult to remember all the commands. To overcome this linux has some of the easy ways to identify the commands. When you are in the linux shell prompt, just by typing starting few of the alphabets you can get the full command in the prompt. For this you need to use the key. If many commands starting by that alphabets exists, linux will list all the commands starting with alphabets given.

Example1:
$what
$whatis

Example2:
$a
Display all 127 possibilities? (y or n)
a2p aleph ar atq
a2ping alias arch atrm
a2ps allcm arecord atrun
ab allec arecordmidi attr
ac allneeded ark audiofile-config
accept alsacard arp audit2allow
....
....

$a

Example3:
$wh
whatis which whiptail whoami
whereis while who whois
$wh

Sometime you may not know the command usage. Every command has its own help page and manual.

To get the command help

$--help

For example to get the 'ls' command help page.

$ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
....
....

To get the command manual.


$man ls #This command will get you to the manual page of the 'ls'

LS(1) User Commands LS(1)

NAME
ls - list directory contents

SYNOPSIS
ls [OPTION]... [FILE]...

DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.

Mandatory arguments to long options are mandatory for short options
too.
....

....

:q

To come out of the manual page we have to use ':q', just like vi editor command.

If you want to know what for that command is used in a single line. You can use this command

$whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents

$whatis grep
grep (1) - print lines matching a pattern
grep (1p) - search a file for a pattern
grep (rpm) - The GNU versions of grep pattern matching utilities.
If you want to know where does that command exists. I mean the directory under this command lies and its orgin. You can use the following command.

To find out the directory and the origin of the command.

$whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz

$whereis apachectl
apachectl: /usr/sbin/apachectl /opt/CollabNet_Subversion/bin/apachectl /usr/share/man/man8/apachectl.8.gz

If you want all the relevant files (binary,manuals etc) pertaining to the command. Use the following command

$locate ls #This command is works only redhat based linux OS
/bin/alsacard
/bin/alsaunmute
/bin/false
/bin/ls
/boot/grub/menu.lst
/etc/alsa
/etc/lsb-release.d
/etc/mtools.conf
/etc/protocols
/etc/redhat-lsb
/etc/shells
/etc/alsa/ainit.conf
/etc/alsa/alsa.conf
/etc/alsa/cards
/etc/alsa/pcm
/etc/alsa/sndo-mixer.alisp
/etc/alsa/cards/AACI.conf
....
....

whenever you install new tool or package or utility. For that the above command will not work. For example if apache is newly installed in linux. The following command will not return anything.

$locate apachectl

Under this circumstances, you have to use the command updatedb. This command can be executed only by the superuser. This command updates the indexes in the db where the all the commmand directory details stored. After this you will be able to get where exactly new installed binaries lies.

$su #acuquires super user
#updatedb
^d #logoff
$locate apachectl
/etc/opt/CollabNet_Subversion/default-site/htdocs/manual/programs/apachectl.html
/etc/opt/CollabNet_Subversion/default-site/htdocs/manual/programs/apachectl.html.en
/etc/opt/CollabNet_Subversion/default-site/htdocs/manual/programs/apachectl.html.ko.euc-kr
/home/anand/httpd-2.2.6/docs/man/apachectl.8
/home/anand/httpd-2.2.6/docs/manual/programs/apachectl.html
/home/anand/httpd-2.2.6/docs/manual/programs/apachectl.html.en
/home/anand/httpd-2.2.6/docs/manual/programs/apachectl.html.ko.euc-kr
/home/anand/httpd-2.2.6/support/apachectl
/home/anand/httpd-2.2.6/support/apachectl.in
...

To get the all the aliases and the directories where and all the executable exist, the following command can be used.

$which ls
alias ls='ls --color=tty'
/bin/ls

--
Anand