How to manage stunnel process without super daemon in linux

1 minute read

start/stop shell script to run stunnel

Shell script for managing stunnel (stunnel.sh)

#!/bin/bash

USAGE_PARAM="{start/stop/restart/log}"
CUR_DATE=`date '+%Y%m%d%H%M%S'`
STUNNEL_SHELL=`basename $0`

echo "---------------------------------------------------"
echo "START : "`date '+%Y-%m-%d %H:%M:%S'`
echo "---------------------------------------------------"

if [ $# = 1]
then
    echo "$STUNNEL_SHELL $1 is starting..."
elif [ $# = 2 ]
then
    echo "$STUNNEL_SHELL $1 $2 is starting..."
else
    echo "Usage : $STUNNEL_SHELL $USAGE_PARAM"
    exit
fi

echo "Current processes : "`ps -e|grep stunnel|grep -v grep|grep -v $STHNNEL_SHELL`

running_count ()
{
    ps -e|grep stunnel|grep -v grep|grep -v $STUNNEL_SHELL|wc -l
}

start_stunnel ()
{
    if [ $(running_count) = 0 ]
    then
        backup_log

        # Change the stunnel path whatever you want
        $HOME/sunnel/bin/stunnel $HOME/stunnel/data/stunnel.conf
    else
        echo "stunnel is already running. (Running count:$(running_count))"
    fi
}

stop_sunnel ()
{
    for pid in $(ps -e|grep stunnel|grep -v grep|grep -v $STUNNEL_SHELL|awk '{print $1}')
    do
        echo "kill -TERM "$pid
        kill -TERM $pid
    done
}

restart_stunnel ()
{
    stop_stunnel
    sleep 1
    start_stunnel
}

backup_log ()
{
    mv $HOME/stunnel/log/stunnel.log $HOME/stunnel/log/stunnel.$CUR_DATE.log
}

tail_log ()
{
    tail -100f $HOME/stunnel/log/stunnel.log
}

case $1 in
    restart)
        restart_stunnel
        ;;
    start)
        start_stunnel
        ;;
    stop)
        stop_stunnel
        ;;
    log)
        tail_log
        ;;
    *)
    echo "Usage : $STUNNEL_SHELL $USAGE_PARAM"
    exit
esac

Add jobs to cron under Linux or Unix (crontab -l)

# Monitor the stunnel process
* * * * * $HOME/stunnel/shl/stunnel.sh start >> $HOME/stunnel/log/stunnel_cron.log 2>&1

# Delete crontab log
* 5 * * 0 cp /dev/null $HOME/stunnel/log/stunnel_cron.log > /dev/null 2>&1

Configuration of stunnel (stunnel.conf)

;------------------------------------------
; Global options
;------------------------------------------
pid=$HOME/stunnel/data/stunnel.pid
output=$HOME/stunnel/log/stunnel.log

;socket = a:TCP_NODELAY=1
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
socket = l:SO_KEEPALIVE=1
socket = r:SO_KEEPALIVE=1
forground = no
debug = 7
syslog = yes

;------------------------------------------
; Service-level options
;------------------------------------------
[stunnel1]
client = yes
accept = 127.0.0.1:12701
connect = xxx.xxx.xxx.xxx:1234
TIMEOUTclose = 0
verify = 2
sslVersion = all
cert = $HOME/stunnel/data/sess1/pem/cert.pem
key = $HOME/stunnel/data/sess1/pem/key.pem
CAFile = $HOME/stunnel/data/sess1/pem/CACerts.pem

[stunnel2]
client = yes
accept = 127.0.0.1:12702
connect = xxx.xxx.xxx.xxx:1234
TIMEOUTclose = 0
verify = 2
sslVersion = all
cert = $HOME/stunnel/data/sess2/pem/cert.pem
key = $HOME/stunnel/data/sess2/pem/key.pem
CAFile = $HOME/stunnel/data/sess2/pem/CACerts.pem

Tags:

Categories:

Updated:

Leave a comment