Thursday 20 February 2014

Tracking All The Background Process in a shell script

Let's assume we have a scenario to write a shell script, where need to execute some statements  in parallel, after finishing those need to execute few more statements.

for example if i have a shell script,

cmd1 &
cmd2 &
cmd3 

i want to execute cmd3 after finishing cmd1 and cmd2.

This scenario may look simple, but it is not.

When i run statements in parallel using & operator, then that statement will be executed in background, so the other set of statements will be executed immediately, these will not wait for background processes.

i have solved this problem with the below script,

i am running one infinite loop, there i am tracking all the background processes, if all the background processes are done, then only i am running the final statements.

cmd1 &
cmd2 &
while true
do
if [ `jobs | grep Running | wc -l` -eq 0 ]; then
cmd 3
break;
fi;
done















Wednesday 19 February 2014

Checking Oracle Connection Status Using Shell Script

We can check the oracle status in a number of ways using shell script.

I am mentioning one of the ways to check the connectivity,

Whenever we connect to oracle database successfully, then it will produce success message which contains "Connected to" as sub-string, i am checking for this string using grep command


  if sqlplus schemaname/password@databasename < /dev/null | grep 'Connected to'; then

      echo "Database Connection is OK .......Starting Export Process ...."

 else
  
      echo "Database Connection is not successful .." 

      exit;



Monday 17 February 2014

Setting up Passwordless SSH

SSH is often used to login from one system to another without requiring passwords. This will be required when you run a cluster, it will not ask you for the password again and again.


 steps:

1. First log in on Host1 with user user1 and generate a pair of authentication keys. 
    Do not enter a passphrase:

    user1@Host1:~> ssh-keygen –t rsa


          2. Now use ssh to create a directory ~/.ssh as user user2 on Host2 .
                ( if directory already exists, then no issues with that)

     user1@Host1:~>ssh user2@Host2 mkdir –p .ssh

     user2@Host2’s password:

3. Finally append Host1's new public key to user2@Host2:.ssh/authorized_keys and 
     enter Host2's password one last time:

     user1@Host1:~>cat /home/hadoop/.ssh/id_rsa.pub | ssh user2@Host2 'cat >>         .ssh/authorized_keys'

    hadoop@Host2’s password:

    now you can login to Host2 using user2 from Host1 machine

    user1@Host1:~>ssh user2@Host2 hostname

   NOTE: If you face any issue while logging in, please make the below changes:
    •    Put the public key in .ssh/authorized_keys2
    •     Change the permissions of .ssh to 700
    •      Change the permissions of .ssh/authorized_keys2 to 640