web-dev-qa-db-fra.com

script de démarrage init.d

J'essaie de créer un script de service init.d pour le logiciel glassfish. Mais il semble que mes compréhensions dans les directives init LSB ne sont pas les meilleures.

Ce sont les commandes que le script doit faire:

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

Mon script ressemble à ceci mais cela n'a pas fonctionné. Ceci est ma première tentative de créer un script init.d. S'il vous plaît dites-moi quand je fais quelque chose de mal.

Remarque: Veuillez regarder mon script mis à jour ci-dessous 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

:       

Lorsque je fais un '/ bin/bash -x ./glassfish status', voici le résultat:

+ 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

Mais peu importe si je commence ou arrête. Le résultat est toujours le même. Le script ne démarre pas le serveur de domaine glassfish. Sans script, tout fonctionne bien.

MODIFIER:

J'ai changé le script en ceci:

#!/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

Après le changement, la sortie est la suivante:

 * 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.

Il peut commencer le processus maintenant. Ensuite, le problème suivant apparaît lors de l'arrêt du service:

 * glassfish process is not running

Mais le processus est en cours d'exécution et le script n'essaie même pas d'abandonner. Il n’existe pas non plus de fichier PID glassfish sous/var/run.

1
user3772108

Enfin je le fais travailler!

Si quelqu'un s'intéresse à la réponse. Le problème est que Glassfish n'a pas créé de fichier PID par lui-même. Il existe donc une solution de contournement nécessaire pour démarrer le programme en arrière-plan (avec &) et générer son PID.

Pour la compréhension, j'ai regardé ces messages:

La réponse de Patrick

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

la réponse de l0b0

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

la réponse de kojiro

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

Stack * (askUbuntu) -community est vraiment une grande communauté! :)

Et voici le script de travail complet:

#!/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

Comme il se trouve sous /etc/init.d/, je ne suis pas sûr que le script ait encore besoin de la section INIT INFO.

Maintenant, il est possible de l'utiliser comme service normal comme ceci:

Sudo service glassfish start

Puis ajoutez-le au démarrage de rc et il démarrera après le redémarrage.

Sudo update-rc.d glassfish defaults
1
user3772108