Running Glassfish as a service on CentOS

Here is how you run glassfish as a service on CentOS:

  1. Create a user glassfish (you can call it anything you want) under which Glassfish will run.
    #useradd glassfish
  2. Install glassfish in /home/glassfish.
  3. Create the startup script /etc/init.d/glassfish for glassfish.
    #!/bin/bash
    #
    # glassfish: Startup script for Glassfish Application Server.
    #
    # chkconfig: 3 80 05
    # description: Startup script for domain1 of Glassfish Application Server.

    GLASSFISH_HOME=/home/glassfish/glassfish;
    export GLASSFISH_HOME

    GLASSFISH_OWNER=glassfish;
    export GLASSFISH_OWNER

    start() {
            echo -n "Starting Glassfish: "
            echo "Starting Glassfish at `date`" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            su $GLASSFISH_OWNER -c "$GLASSFISH_HOME/bin/asadmin start-domain domain1" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            sleep 2
            echo "done"
    }

    stop() {
            echo -n "Stopping Glassfish: "
            echo "Stopping Glassfish at `date`" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            su $GLASSFISH_OWNER -c "$GLASSFISH_HOME/bin/asadmin stop-domain domain1" >> $GLASSFISH_HOME/domains/domain1/logs/startup.log
            echo "done"
    }

    # See how we were called.
    case "$1" in
            start)
                    start
                    ;;
            stop)
                    stop
                    ;;
            restart)
                    stop
                    start
                    ;;
            *)
                    echo $"Usage: glassfish {start|stop|restart}"
                    exit
    esac
    view raw glassfish This Gist brought to you by GitHub.
  4. Install the service
    #chmod +x /etc/init.d/glassfish
    #chkconfig --add glassfish
    #chkconfig --level 3 glassfish on
  5. Start glassfish.
    #/etc/init.d/glassfish start
This entry was posted in Codeprix and tagged , , , . Bookmark the permalink.

12 Responses to Running Glassfish as a service on CentOS

  1. nairdaen says:

    I think you’re missing a “-” in the line “#chkconfig -add glassfish” so that it is “#chkconfig –add glassfish”

  2. anand says:

    @nairdaen
    fixed

  3. Thank you, I’ve modified this script for our own Glassfish installations.

  4. the birdie says:

    Awesome! Thanks for the knowledge.

  5. Maurizio says:

    Hi, thank you for this script…

    when i enter #/etc/init.d/glassfish start, system tell me:
    CLI304 Can’t find domain.xml. It should be here: /usr/glassfishv3/glassfish/domains/domain1/config/domain.xml
    Command start-domain failed.

    If i call #asadmin start-domain domain1 from super user, glassfish run correctly

    Can you help me?

  6. anand says:

    @Maurizio

    There might be a permission problem with domain.xml file. Try chmod.

  7. Huub Daems says:

    Great help this script. Used it as a base. There is a little typo in the location of the startup.log it should be $GLASSFISH_HOME/glassfish/domains/domain1/logs/startup.log

  8. aim says:

    a great tutorial, thank you very much.

  9. caospy says:

    muchas Gracias por el Post , Excelente.

  10. Fahmi Mohamed says:

    Thank you Anand! This is a very useful post.

  11. Albert says:

    Ther is no domain.xml file at /usr/glassfishv3/glassfish/domains/domain1/config/
    No permission problem, the file is not there !!!
    Any sugestions ?
    Thanks
    Albert

  12. Albert says:

    I’m sorry !!!
    The file was there…
    It was actualy a permission problem.
    Thanks and sorry again.
    Albert

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>