178 lines
3.7 KiB
Bash
Executable File
178 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
check_string_ps=""
|
|
if [[ -f "/usr/sbin/openvpn" ]] ; then
|
|
check_string_ps="/usr/sbin/openvpn --daemon"
|
|
fi
|
|
|
|
|
|
# - used, if systemd is NOT supported
|
|
init_script=""
|
|
if [[ -x "/etc/init.d/openvpn" ]] ; then
|
|
init_script="/etc/init.d/openvpn"
|
|
fi
|
|
|
|
# - Used if systemd is supported
|
|
# -
|
|
service_name=openvpn
|
|
|
|
LOCK_DIR=`mktemp -d`
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Base Function(s)
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
fatal(){
|
|
echo ""
|
|
echo -e " [ Fatal ] $*"
|
|
echo ""
|
|
echo -e "\tScript terminated.."
|
|
echo ""
|
|
rm -rf $LOCK_DIR
|
|
exit 1
|
|
}
|
|
|
|
error (){
|
|
echo ""
|
|
echo -e " [ Error ] $*"
|
|
echo ""
|
|
}
|
|
|
|
warn (){
|
|
echo ""
|
|
echo -e " [ Warn ] $*"
|
|
echo ""
|
|
}
|
|
|
|
info (){
|
|
echo ""
|
|
echo -e " [ Info ] $*"
|
|
echo ""
|
|
}
|
|
|
|
ok (){
|
|
echo ""
|
|
echo -e " [ Ok ] $*"
|
|
echo ""
|
|
}
|
|
|
|
trim() {
|
|
local var="$*"
|
|
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
|
|
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
|
|
echo -n "$var"
|
|
}
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Check some prerequisites
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
# - Running in a terminal?
|
|
# -
|
|
if [[ -t 1 ]] ; then
|
|
terminal=true
|
|
LOGGING=true
|
|
else
|
|
terminal=false
|
|
LOGGING=false
|
|
fi
|
|
|
|
# - Running in a terminal?
|
|
# -
|
|
if [[ -t 1 ]] ; then
|
|
terminal=true
|
|
LOGGING=true
|
|
else
|
|
terminal=false
|
|
LOGGING=false
|
|
fi
|
|
|
|
if [[ -z $check_string_ps ]]; then
|
|
fatal "$(basename $0): OpenVPN Service seems NOT to be installed"
|
|
fi
|
|
|
|
# - Systemd supported ?
|
|
# -
|
|
systemd=$(which systemd)
|
|
systemctl=$(which systemctl)
|
|
|
|
systemd_supported=false
|
|
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
|
systemd_supported=true
|
|
else
|
|
if [[ ! -x $init_script ]]; then
|
|
fatal "$(basename $0): Missing Bind Init-Script!"
|
|
fi
|
|
fi
|
|
|
|
|
|
#---------------------------------------
|
|
#-----------------------------
|
|
# Check if OpenVPN Service is running
|
|
#-----------------------------
|
|
#---------------------------------------
|
|
|
|
if $LOGGING ; then
|
|
echo -e "\n Check if OpenVPN Service is running.."
|
|
echo -e " ====================================="
|
|
fi
|
|
|
|
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}')
|
|
if [[ "X${PID}" = "X" ]]; then
|
|
|
|
error "OpenVPN Service seems to be down! Trying to restart service now.."
|
|
|
|
if $systemd_supported ; then
|
|
$systemctl stop $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log
|
|
if [[ $? -ne 0 ]]; then
|
|
error "$(cat ${LOCK_DIR}/err_msg.log)"
|
|
fi
|
|
sleep 10
|
|
$systemctl start $service_name > /dev/null 2> ${LOCK_DIR}/err_msg.log
|
|
if [[ $? -ne 0 ]]; then
|
|
error "$(cat ${LOCK_DIR}/err_msg.log)"
|
|
fi
|
|
else
|
|
$init_script stop > /dev/null 2>&1
|
|
if [[ $? -ne 0 ]]; then
|
|
error "Stopping OpenVPN Service failed!"
|
|
fi
|
|
sleep 10
|
|
$init_script start > /dev/null 2>&1
|
|
if [[ $? -ne 0 ]]; then
|
|
error "Starting OpenVPN Service failed!"
|
|
fi
|
|
fi
|
|
|
|
declare -i counter=0
|
|
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}')
|
|
while [[ "X${PID}" = "X" ]]; do
|
|
sleep 1
|
|
PID=$(ps -e f | grep -E "[[:digit:]]\ ${check_string_ps}"| grep -v grep | awk '{print$2}')
|
|
if [[ $counter -gt 10 ]]; then
|
|
break
|
|
else
|
|
((counter++))
|
|
fi
|
|
done
|
|
|
|
if [[ "X${PID}" = "X" ]] ; then
|
|
error "Restarting OpenVPN Service failed!"
|
|
else
|
|
ok "OpenVPN Service is up and running."
|
|
fi
|
|
|
|
else
|
|
if $LOGGING ; then
|
|
ok "OpenVPN Service is up and running."
|
|
fi
|
|
fi
|
|
|
|
exit 0;
|