Impostare Glassfish come servizio di sistema non privilegiato

Una volta installato Glassfish ci sono ancora due cose da fare:

  • impostare il server come servizio di sistema
  • creare un utente, senza privilegi amministrativi, per far girare server e applicativi

Come prima cosa creiamo un utente, con una sua home:
$> sudo useradd -s /bin/bash -d /home/glassfish -m -g glassfish glassfish

Con questo stesso utente procediamo all’installazione del server, nel mio caso è installato su /opt (una partizione che lascio separata), si apre una shell e, nell’ordine:
$> su glassfish
$> cd
$> wget $glassfish
$> cd /opt
$> unzip glassfish.zip

Usciamo dalla shell e, come utente privilegiato, prepariamo il seguente script:
$> vi glassfish

#! /bin/sh

GLASSFISHPATH=/opt/glassfishv3/glassfish/bin
GLASSFISHUSER=glassfish

case "$1" in
start)
echo "starting glassfish from $GLASSFISHPATH"
sudo -u $GLASSFISHUSER $GLASSFISHPATH/asadmin start-domain domain1
;;
restart)
$0 stop
$0 start
;;
stop)
echo "stopping glassfish from $GLASSFISHPATH"
sudo -u $GLASSFISHUSER $GLASSFISHPATH/asadmin stop-domain domain1
;;
*)
echo $.usage: $0 {start|stop|restart}.
exit 3
;;
esac

Riavviate la macchina e assicuratevi che l’istanza di glassfish sia viva e che stia girando con l’utente corretto.

Nella home dell’utente glassfish lascio comunque sempre una serie di script che utilizzo per eseguire varie operazioni come riavvio, riavvio con full undeploy e redeploy degli applicativi installati, query varie ai servizi UMS e via dicendo, alcuni esempi:

check_server.sh (schedulato tre volte al giorno)

#!/bin/sh
#
# FILENAME:     check_server.sh
#
# AUTHOR:       youremailaddress
#
# DATE:         12/10/2010
#
# SCOPE:        Check Glassfish status
#
############################################################################

DATE=$(date '+%x %H:%M')
SUBJECT="Glassfish Server Status Check $DATE"
MAIL=mail_body.txt

# Aliases for commands
alias glass='/opt/glassfishv3/glassfish/bin/asadmin --user admin --passwordfile=/home/glassfish/.passwd/glassfish_passwd.txt'
alias mq='/opt/glassfishv3/mq/bin/imqcmd -u admin -passfile /home/glassfish/.passwd/mq_passwd.txt'

if [ -f /etc/profile ] ; then
    . /etc/profile
fi

# Clean previous mail
rm -rf /home/glassfish/$MAIL

# JDBC Checks
echo "JDBC Pool Status" >> $MAIL
CONNFREE=$(glass get -m *jdbc_resource* | grep -e "numconnfree-current" | awk '{print $3}')
CONNUSED=$(glass get -m *jdbc_resource* | grep -e "numconnused-current" | awk '{print $3}')
MAXCONNUSED=$(glass get -m *jdbc_resource* | grep -e "jdbc_resource.numconnused-highwatermark" | awk '{print $3}')
CONNLEAK=$(glass get -m *jdbc_resource* | grep -e "jdbc_resource.numpotentialconnleak-count" | awk '{print $3}')

echo "Total free connections: " $CONNFREE >> $MAIL
echo "Total used connections: " $CONNUSED >> $MAIL
echo "Maximum concurrent connections: " $MAXCONNUSED >> $MAIL
echo "Potential connection leaks (must be zero): " $CONNLEAK >> $MAIL

# Threads Checks
echo "\nThread status" >> $MAIL
THREADSUSED=$(glass get -m *thread* | grep -e "jvm.thread-system.threadcount-count" | awk '{print $3}')
THREADDEAD=$(glass get -m *thread* | grep -e "monitordeadlockedthreads-current" | awk '{print $3}')

echo "Total running threads: " $THREADSUSED >> $MAIL
echo "Total deadlocked threads: " $THREADDEAD >> $MAIL

# JMS Checks
echo "\nJMS status" >> $MAIL
JMSCOMPLETED=$(glass get -m *jms* | grep -e "jms-service.work-management.completedworkcount-count" | awk '{print $3}')
JMSWAITING=$(mq query bkr| grep -e "Current Number of Messages in System" | awk '{print $7}')

echo "Total completed tasks: " $JMSCOMPLETED >> $MAIL
echo "Total waiting tasks: " $JMSWAITING >> $MAIL

# Message Queue Checks
echo "\nDead Message Queue Status" >> $MAIL
DEADMSGS=$(mq query bkr | grep -e "Current Number of Messages in Dead Message Queue" | awk '{print $9}')
DEADMSGSSIZE=$(mq query bkr | grep -e "Current Total Message Bytes in Dead Message Queue" | awk '{printf("%.1f %s",$9/1024,"kbytes")}')

echo "Total messages in dead queue: " $DEADMSGS >> $MAIL
echo "Dead messages queue size: " $DEADMSGSSIZE >> $MAIL

# Send the mail
mailx -s "$SUBJECT" "youremailaddress" < $MAIL

# Clean files
rm -rf /home/glassfish/$MAIL

redeploy_application.sh

#!/bin/sh
#
# FILENAME:     redeploy_application.sh
#
# AUTHOR:       yourmailaddress
#
# DATE:         12/10/2010
#
# SCOPE:        Restarts the application server with full undeploy
#               and redeploy of all components
#
############################################################################

if [ -f /etc/profile ] ; then
    . /etc/profile
fi

# Display help
if [ -z "$1" ]; then
        echo usage: $0 moduleName
        exit
fi

# Define alias
alias glass='asadmin --user admin --passwordfile=/home/glassfish/.passwd/glassfish_passwd.txt'
# Get module to be redeployed
MODULE=$1

echo "Undeploying application $MODULE:"
glass undeploy $MODULE

echo "Redeploying application $MODULE:"
glass deploy /home/glassfish/ejb/$MODULE/$MODULE.ear

Leave a Reply

Your email address will not be published. Required fields are marked *