[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Petit script



Le 30/05/2012 13:23, Nahliel Steinberg a écrit :
bonjour,

Je cherche le moyen de relancer un service automatiquement quand il tombe, pour qu'il soit
relancé automatiquement.


Exemple pour relancer les VPN. A mettre dans cron toutes les Y minutes de suivi désiré. Il permet de gérer plusieurs services.

#!/bin/sh
#
# Creation 2012/03
# Daniel Huhardeaux - devel@tootai.net
#

debug=no
emulate=no

START=/usr/sbin/service
PGREP=/usr/bin/pgrep
PS="/bin/ps -fp"
backupIFS=$IFS
IFS=" "

DAEMON_TO_FOLLOW="openvpn"
export openvpn_SERVICES="vpn1 vpn2 vpn3 ... vpnX"

for daemon in $DAEMON_TO_FOLLOW; do
    daemon_SERVICES=${daemon}_SERVICES

[ "$debug" = "yes" ] && echo "Daemon=$daemon Services=$daemon_SERVICES Value=$openvpn_SERVICES"

    if [ "$openvpn_SERVICES" != "" ]; then

    for service in $openvpn_SERVICES; do
        [ "$debug" = "yes" ] && echo -n "$daemon: $service"

        ps -fp $($PGREP -d, -f $service) >/dev/null 2>&1
        if [ $? -ne 0 ]; then
        # service not running, we restart service
[ "$debug" = "yes" ] && echo " NOT RUNNING ($START $daemon restart $service)" [ "$emulate" != "yes" ] && sudo $START $daemon restart $service >/dev/null 2>&1 && logger "$daemon RESTART $service"
        else
        [ "$debug" = "yes" ] && echo " OK ($PS $($PGREP -d, -f $service))"
        fi
    done
    else
    [ "$debug"    = "yes" ] && echo "ps -fp $(pgrep -d, -x $daemon)"
[ "$emulate" != "yes" ] && ps -fp $(pgrep -d, -x $daemon) >/dev/null 2>&1
    fi
done

IFS=$backupIFS

exit 0

--
Daniel


Reply to: