Wednesday, March 31, 2010

What is crontab?

   UNIX operating systems contain a daemon called 'cron'. The job of the cron daemon is to wake up every minute and execute all the tasks scheduled for the particular minute.  The tasks to be executed by the cron are present in a file called 'crontab' file.

   The crontab file is present for every user in the UNIX box.  Generally, shell scripts to be executed on a timely basis is scheduled as part of the crontab. However, even standalone UNIX commands can also be scheduled as part of the crontab.

  UNIX has a command called 'crontab'. This crontab command can be used to create new crontab tasks and also to check the tasks scheduled in cron.

crontab usage:

  1. To check the crontab activities scheduled,

    #crontab -l
    4 5 * * sat     echo "Execute at 4 after 5 every saturday"
    #

    The above displayed task will execute at 4hours 5 mins on every saturday.

  2. To create a new task in crontab;

   #crontab -e
    20 00 * * *  tcsh ~gpr/bin/create_ctags v94_0
   #

    On typing the command 'crontab -e', a vi like editor opens up. We need to edit the file and put the above shown contents as editing any file in vi. And finally, save and quit the file. The above command creates a cron job to execute at 20hours everyday.

  Syntax of the crontab file:

   hh mm dom mon dow  

    hh   - hour   (0-24)
    mm - minute (0-59)
    dom - day of the month   (1-31)
    mon - month  (0-12)
    dow  - day of the week (0-6   0-Sunday  6-Saturday. Also, we can use the names, like sun, mon)

  The following are the important things to keep in mind:

  1. While testing cron activities, dont schedule any activity in the immediate next minute. Schedule atleast 2mins later. At times, cron does not refresh properly.

  2. When cron executes any crontab activity, it spawns a new shell and executes. This new shell does not inherit all the environment variables of the user. Instead, very few environment variables are set. The following are few of them: SHELL(/bin/sh), USER, LOGNAME and HOME

3. Because of the above reason, a shell script running successfully at the command prompt may not run successfully when scheduled in crontab. In order to overcome this, the user has to include the sourcing of his .login or .profile file inside the shell script.

Saturday, March 27, 2010

What is Shell Scripting?

  Shell script is nothing but a batch file of windows.  In layman's term, a set of UNIX commands put together in a file becomes a shell script. In addition, the shell also provides additional programming  logic handling thereby making it more useful.


  Shell scripting or programming is typically used when we want to automate tasks which we do repeatedly. Once a shell script is created for a task or activity, just the script needs to be run in order to perform the task from next time onwards. Undoubtedly, shell script saves lot of time for the end user.


  A good Shell programmer is one who has a very good grip on the UNIX command line interface. A Shell programmer with not a good knowledge of UNIX command line will end up writing big scripts, which actually could have been done using some UNIX commands itself.

Let's  see how to write a simple shell script to display the 'Hello World' :

1. Write a script file hello.sh with the following contents as shown below:

    # cat hello.sh
       #!/usr/bin/ksh
       echo "Hello World"
    #

 2.  Give executable permission to the file:

   #chmod u+x hello.sh

 3. Run the script:

   #./hello.sh
   Hello World
   #


The following things are to be noted:

1. The first line '/usr/bin/ksh' tells in which shell this script should be run.
2. This line is not mandatory. If this line is not present, the script will be run by the default shell of the user.
3. Every script should be given the executable permission in order to execute it.
4. ./hello.world tells the shell that the script 'hello.world' to be executed is present in the current directory.

 'Happy scripting'.

Sunday, March 7, 2010

VIM Installation

 This article tells you about the steps to install VIM in a UNIX box.  VIM is a very powerful tool when it comes to file editing. VIM works on top of vi. It has hundreds of features which will be handy for a everybody working in unix. There are hundreds of vim plugins available as well.  The following steps tells us the steps to install vim.

1. Download the latest vim version from the vim.org website: http://www.vim.org/download.php

2.  Unzip the vim file:
    #gunzip vim-7.2.tar.gz


   The file vim-7.2.tar got created.

3. Untar the tar file created in the previous step:
   #tar -xvf vim-72.tar

   A directory vim-72 would have got created.  Do a cd into the vim-72 directory.

4. Run the configure file .
    #./configure


    A make file would have got created.

5.  Before running the make file, ensure from the command prompt the variables $CC, $CFLGAGS and $LDFLAGS are unset.

    #make


  An exe with the name vim would have got created.

6.  Update the PATH variable to add the path where the vim exe is located.