1

I try to create a init.d service script for the glassfish software. But it seems like my understandings in the LSB init.d guidelines are not the best.

This are the commmands the script should do:

/opt/glassfish/bin/asadmin start-domain
/opt/glassfish/bin/asadmin stop-domain
/opt/glassfish/bin/asadmin restart-domain

My script looks like this but it didn't work. This is my first try to make a init.d script. Please tell me when I do something wrong.

Note: Please look my updated script below EDIT:

#!/bin/sh
#
### BEGIN INIT INFO
#
# Provides:     glassfish
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:    $local_fs $remote_fs $network
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Glassfish scipt (Non official)
# Description:  Start Glassfish domain as service.
#       Non official startup script.
#
### END INIT INFO

BASE=/opt/glassfish/bin
DEAMON=${BASE}/asadmin
USERID=root
NAME=glassfish
DESC="Glassfish domain service"
# PID file for the deamon
PIDFILE=/var/run/glassfish.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DEAMON" ] || exit 0

# Using LSB functions to perform the operations
. /lib/lsb/init-functions

do_start()
{
    start-stop-deamon --start --quiet --pidfile $PIDFILE --exec $DEAMON start-domain -- $NAME_OPTIONS
}

do_stop()
{
    start-stop-deamon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --exec $DEAMON stop-domain
}

case $1 in
    start)
        if init_is_upstart; then
            exit 1
        fi
        log_deamon_msg "Starting $DESC"
        do_start
        case "$?" in
            0) sendsigs_omit 
               log_end_msg 0 ;;
            1) log_progress_msg "already started"
               log_end_msg 0 ;;
            *) log_end_msg 1 ;;
        esac

        ;;
    stop)
        if init_is_upstart; then
            exit 0
        fi
        log_deamon_msg "Stopping $DESC"
        do_stop
        case "$?" in
            0) log_end_msg 0 ;;
            1) log_progress_msg "already stopped"
               log_end_msg 0 ;;
            *) log_emd_msg 1 ;;
        esac

        ;;
    restart|force-reload)
        if init_is_upstart; then
            exit 1
        fi
        $0 stop
        $0 start
        ;;
    status)
        status_of_proc -p $PIDFILE $DEAMON && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
        exit 3
        ;;
esac

:       

When I do a '/bin/bash -x ./glassfish status' this is the output:

+ case "$FANCYTTY" in
+ true
++ /usr/bin/tput setaf 1
+ RED=''
++ /usr/bin/tput op
+ NORMAL=''
+ echo ' *  is not running'
 *  is not running
+ return 3
+ exit 3

But it doesn't matter if I do a start or stop. The result is always the same. The script doesn't start the glassfish domain server. Without script everything works fine.

EDIT:

I changed the script to this:

#!/bin/sh

### BEGIN INIT INFO
#
# Provides:     glassfish
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:    $local_fs $remote_fs $network
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Glassfish scipt (Non official)
# Description:  Start Glassfish domain as service.
#       Non official startup script
#
### END INIT INFO

# Using the LSB functions to perform the operations
. /lib/lsb/init-functions

BASE=/opt/glassfish/bin
NAME=glassfish
DAEMON=${BASE}/asadmin
SCRIPTNAME=/etc/init.d/$NAME

#PID file for the daemon
PIDFILE=/var/run/glassfish.pid

#If the daemon is not there, then exit
[ -x "$DAEMON" ] || exit 5

do_start()
{
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON start-domain
}

do_stop()
{
    start-stop-daemon --stop --quiet --pidfile $PIDFILE
}

case $1 in
    start)
        #Check PID file
        if [ -e $PIDFILE ]; then
            status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
            # IF SUCCESS dont start again
            if [ $status = "0" ]; then
                exit
            fi
        fi

        #Start the daemon
        log_daemon_msg "Starting the process" "$NAME"
        if do_start; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;

    stop)
        # Stop the daemon
        if [ -e $PIDFILE ]; then
            status_of_proc -p $PIDFILE $DAEMON "Stopping the $NAME process" && status="0" || status="$?"
            if [ "$status" = 0]; then
                do_stop
            fi
        else
            log_daemon_msg "$NAME process is not running"
            log_end_msg 0
        fi
        ;;

    restart)
        # Restart the daemon
            $0 stop && sleep 2 && $0 start
        ;;

    status)
        # Check status
        if [ -e $PIDFILE ]; then
            status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
        else
            log_daemon_msg "$NAME Process is not running"
            log_end_msg 0
        fi
        ;;

    *)
        # Show help
        echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
        exit 3
        ;;
esac

After the change the output is this:

 * Starting the process glassfish                                                                                                                                                                                                            Waiting for domain1 to start .........
Successfully started the domain : domain1
domain  Location: /opt/glassfish/domains/domain1
Log File: /opt/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.

It can start the process now. Then the next problem appears when stopping the service:

 * glassfish process is not running

But the process is running and the script does not even try and just abort. There is also no glassfish PID file under /var/run.

  • Is there anything interesting in /var/log/upstart/glassfish* or thereabouts? – Jos Nov 18 '15 at 14:00
  • Status) and *) are working. – user3772108 Nov 18 '15 at 14:00
  • No there is no glassfish file. – user3772108 Nov 18 '15 at 14:02
  • And no 'glassfish.pid' file under '/var/run/'. – user3772108 Nov 18 '15 at 14:04
  • 1
    Which version of Ubuntu are you running? – Jos Nov 18 '15 at 14:09
1

Finally I get it working!

If someones interested in the answer. The problem is that the Glassfish didn't create a PID file by itself. So there is a workaround needed where you start the program in the background (with &) and output its PID.

For the understanding I looked this posts:

Patrick's answer

https://unix.stackexchange.com/questions/137519/start-stop-daemon-not-working-as-expected-no-pid-file-was-written

l0b0's answer

https://stackoverflow.com/questions/9890062/how-to-run-a-program-and-know-its-pid-in-linux

kojiro's answer

https://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables

Stack*(askUbuntu)-community is really a great community! :)

And here is the full working script:

#!/bin/sh

### BEGIN INIT INFO
#
# Provides:     glassfish
# Required-Start:   $local_fs $remote_fs $network
# Required-Stop:    $local_fs $remote_fs $network
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description:    Glassfish scipt (Non official)
# Description:  Start Glassfish domain as service.
#       Non official startup script by Bernhard Sumser.
#
### END INIT INFO

# Using the LSB functions to perform the operations
# NOT needed because no LSB functions used
#. /lib/lsb/init-functions

BASE=/opt/glassfish/bin
NAME=glassfish
DAEMON=${BASE}/asadmin
SCRIPTNAME=/etc/init.d/$NAME

#PID file for the daemon
PIDFILE=/var/run/glassfish.pid

#If the DAEMON is not there, then exit
[ -x "$DAEMON" ] || exit 0


do_start()
{
    $DAEMON start-domain &
    # Wait for child to exit before continuing
    wait
    # Make file with last background PID
    echo $! > $PIDFILE

    # Didn't work because the programm prints from the background. Without moving to the bg no $! can be outputed to file
    #(($DAEMON start-domain) & echo $! > $PIDFILE &)
}

do_stop()
{
    $DAEMON stop-domain
    if [ -e $PIDFILE ]; then
        rm -f $PIDFILE
    fi
}

check_root()
{
    if [ "$(id -u)" != "0" ]; then
        echo "You must be root to start, stop and restart $NAME."
        exit 4
    fi
}

check_process()
{
    # Check if the process is already running. Ignore grep line.
    result=`ps aux | grep /opt/glassfish/modules/glassfish.jar | grep -v grep | wc -l`
}

case $1 in
    start)
        check_root
        check_process
        if [ "$result" = "1"  ]; then
            echo "$NAME is already running"
        else
            # Check if PID file exists and delete it
            if [ -e $PIDFILE ]; then
                rm -f $PIDFILE
            else
                do_start
            fi              
        fi
    ;;

    stop)
        check_root
        if [ -e $PIDFILE ]; then
            do_stop
        else
            echo "$NAME is not running"
        fi
    ;;

    restart)
        check_root
        echo "Restarting $NAME..."
        check_process
        if [ "$result" = "1"  ]; then
            do_stop
            echo "Starting $NAME..."
            do_start
        fi                  
    ;;

    status)
        if [ -e $PIDFILE ]; then
            echo "$NAME is running. PID $(cat $PIDFILE)"
        else
            echo "$NAME is not running"
        fi
    ;;


    *)
            # Show help
            echo "Usage: $SCRIPTNAME {start|status|stop|restart}" >&2
            exit 3
    ;;
esac

Since it is located under /etc/init.d/ I am not sure if the script still needs the INIT INFO section.

Now it is possible to use it as normal service like this:

sudo service glassfish start

Then add it to the rc startup and it will start after reboot.

sudo update-rc.d glassfish defaults

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.