Switching context between two instances from one account I have a server with two instances running on it (a dev environment) where we use one login to access either instance.  I wanted to be able to switch context from one instance to the other without changing my .profile to reference the other instance's db2profile, logging out and back in.  I wrote a script (iswap) to allow me to do that.  Here's what it looks like:

  if [[ $DB2INSTANCE = "instance1" ]]
  then
     . /usr/users/instance2/sqllib/db2profile
     echo "Switched context from instance1 to instance2"
  elif [[ $DB2INSTANCE = "instance2" ]]
  then
     . /usr/users/instance1/sqllib/db2profile
     echo "Switched context from instance2 to instance1"
  fi


If I do a "db2 list db directory" after running iswap it shows me the databases in the new instance.  If I echo $DB2INSTANCE it reflects the new instance.  But, if I do a "db2 list applications" it still shows me the applications connected to the original instance.   What am I not changing that I need to add to the script?
The list applications command requires an instance attachment, which is performed implicitly when you first run this command against "instance1". The CLP back-end process db2bp, which handles all server commands and SQL statements on behalf of the front-end program CLP db2, remains attached to the "old" instance after you switch the environment. 

You need to end the back-end process by issuing the terminate command (db2 terminate), which will cause a new back-end process to spawn and attach to the new default instance, as specified by $DB2INSTANCE, next time it is required.