![]() |
|
![]() |
|
Shell Script Snippets |
Home > Resources > HACMP Resources Collection > Shell Script Snippets This page contains various shell script snippets (segments) which may prove useful within customization scripts (eg. application start and start scripts) for IBM's AIX-based HACMP product. The snippets are intended to be used in Korn Shell (/bin/ksh) scripts although they'll probably work in Bourne Shell (/bin/bsh) scripts unless indicated otherwise. These snippets are intended to operate on AIX and were developed and tested on AIX 4.3.2 and/or AIX 4.3.3. Some of the examples below may not work on other Unices or older versions of AIX. If you'd like to help improve this list of shell snippets, click here. This page is part of the Matilda Team's HACMP Resources Collection. The home page of the collection is located here. IMPORTANT: read the disclaimer BEFORE you use any information provided in this collection. Is my shared volume group online?The following sequence will determine if the sharedvg volume group is currently online (often useful in application start scripts):Note the use of the -w option on the grep invocation - this ensures that if you have a sharedvg and a sharedvg2 volume group then the grep only finds the sharedvg line (if it exists). Starting a non-root process from within an application start scriptA common requirement in an application start script is the need to start a program and/or shell script which is to be run by a non-root userid. This snippet does the trick:This will run the startmeup.sh script in a process owned by the dbadmin user. Note that it is possible to pass parameters to the script/program as well: Killing processes owned by a userA common requirement in application stop scripts is the need to terminate all processes owned by a particular user. The following snippet terminates all processes owned by the dbadmin user (this could be part of an application stop script that corresponds to the previous snippet that started the DB as dbadmin).Since a simple kill is rarely enough and a kill -9 is a rather rude way to start a conversation, the following sequence might be useful:DBUSER=dbadmin kill ` ps -u $DBUSER -o pid= `To see how this works, just enter the ps command. It produces output along these lines:DBUSER=dbadmin kill ` ps -u $DBUSER -o pid= ` sleep 10 kill -9 ` ps -u $DBUSER -o pid= `Note that equal sign in the pid= part is important as it eliminates the normal PID title which would appear at the top of the column of output. I.e. without the equal sign, you'd get this:12276 12348Passing PID to the kill command is just a bad idea as writing scripts which normally produce error messages makes it much more difficult to know if things are working correctly.PID 12276 12348 Terminating processes which are using a filesystemYet another common requirement in application stop scripts is the need to terminate all processes using a filesystem to ensure that the unmount of the filesystem works cleanly. The following snippet will terminate (with a SIGKILL signal) all processes using the filesystem in the /dev/sharedlv logical volume:Reminder: make sure that if you use fuser in this fashion, that you specify the name of the filesystem's logical volume and not the filesystem's mount point. If you specify the mount point then you'll kill only processes which happen to be using the mount point directory itself (i.e. you won't get an error message but you won't get the desired result either).fuser -k /dev/sharedlv A more complete example of an application stop scriptA common requirement in application stop scripts is the need to terminate all processes owned by a particular user. For example, a script along the following lines could be used to first gently and then forcibly terminate the database processes started in the previous example: Determining if a process existsSometimes you need to check if a particular process still exists. If you know the process's pid then the following will do the trick (assumes that the shell variable $pid contains the process's pid):This takes advantage of a little used feature of Unix signals. Sending signal 0 to a process never affects the process but the sender of the signal is told if the signal would have been delivered if the signal number had been greater than 0. In the context of the kill command, the command succeeds with an exit status of 0 if a signal could have been delivered and fails with an error message and a non-zero exit status if the signal couldn't have been delivered (the purists will argue that an exit status of 0 from the kill command indicates that the signal was delivered; they'll then point out that the delivery of signal 0 to a process doesn't do anything; Does anybody really care?). Supporting lots of IP addresses within a resource group(the need for this hack has been largely eliminated by the IPAT via IP aliasing feature introduced in HACMP 4.5) IMPORTANT: If you lack the appropriate skills, experience and/or competency, are unwilling to take responsibility for your actions, or if you don't like these disclaimers then don't use this information.
|
| © 2004 Matilda Systems Corporation |