Script to monitor process on Linux

Environment: Oracle Linux 6.2

Sometimes you want to monitor a process on your Linux server and do some alerting when the process is not running anymore.

The following shell script can be used as an example. It will monitor the “dbconsole” process (database control) and send a e-mail message when the process is not running anymore.

$ vi check_db_control.sh

#!/bin/sh

PROCESSFILE='dbconsole'

if [ `ps ax|grep -v grep|grep -ic $PROCESSFILE` -gt 0 ]
then
        echo "$PROCESSFILE is running, everything is fine"
else
        echo "$PROCESSFILE is not running on `hostname -s`!" | mail -s "$PROCESSFILE down!" matthiash@mydomain.com
fi

exit

$ chmod u+x check_db_control.sh

To schedule the script, you can add it to cron. For example, this script will run every day at 10:00AM:

$ crontab -e

# Monitoring dbconsole process
00 10 * * * /home/oracle/scripts/check_db_control.sh 1>/dev/null 2>&1

 

HTH,
Matthias

Advertisements

Automatically start GlassFish 3.1.2 on Oracle Linux 6.2 after server reboot

Environment: GlassFish 3.1.2 Open Source Edition (Web Profile), Oracle Linux 6.2

When you installed GlassFish on Oracle Linux, you probably want to make sure that your Domain Administration Server (DAS) and/or any standalone instances that you created are automatically started after a server reboot. You can do this with a shell script which you add as a service under Linux.

The first step is to create a script under /etc/init.d (with user root):

$ vi /etc/init.d/glassfish

#!bin/bash

# description: glassfish start stop restart
# processname: glassfish
# chkconfig: 2345 20 80

GLASSFISH_HOME=/u01/app/glassfish3
GLASSFISH_USER=oracle
RETVAL=0

case "$1" in
start)
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin start-local-instance instance1"
;;
stop)
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-local-instance instance1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1"
;;
restart)
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin restart-local-instance instance1"
su - $GLASSFISH_USER -c "$GLASSFISH_HOME/bin/asadmin restart-domain domain1"
;;
*)
echo $"Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL

This script will start, stop or restart the DAS with name “domain1” and the local instance with name “instance1” on your Linux server. You should adapt the script for your specific GlassFish instances.

GLASSFISH_HOME is the folder where GlassFish was installed (in my case: /u01/app/glassfish3). GLASSFISH_USER is the name of the user account under which GlassFish is running (in my case: user oracle).

Note: GlassFish needs a JAVA_HOME variable pointing to the folder of your Java binaries. In my case, JAVA_HOME is defined in the .bash_profile of the user “oracle”. You could also add it to the /etc/init.d/glassfish script.

Next, set the correct file permissions on the script:

$ chmod 755 /etc/init.d/glassfish

You can test the script as follows:

$ sh /etc/init.d/glassfish start

Now we need to create a service from the script:

$ chkconfig –add glassfish

This will create a service and start it in the runlevels 2,3,4 and 5. To check this, run the following command:

$ chkconfig|grep glassfish
glassfish 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Let’s check the service:

$ service glassfish stop

Waiting for the instance to stop …
Command stop-local-instance executed successfully.
Waiting for the domain to stop ….
Command stop-domain executed successfully.

Looks ok! The last thing you should test is rebooting your server to see if GlassFish is automatically started after the reboot. Have fun!

Matthias