Redesign scrpt.
This commit is contained in:
parent
5bd4f7fef5
commit
ba0d11c728
@ -1,28 +1,253 @@
|
||||
#"!/usr/bin/env bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
PIDS=`ps aux | grep "/usr/local/samba/sbin/samba" | grep -v grep | awk '{print$2}'`
|
||||
working_dir="$(dirname $(realpath $0))"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Some Variables
|
||||
# -------------
|
||||
|
||||
LOCK_DIR="/tmp/$(basename $0).LOCK"
|
||||
|
||||
|
||||
# -------------
|
||||
# - Job is already running?
|
||||
# -------------
|
||||
|
||||
# - If job already runs, stop execution..
|
||||
# -
|
||||
if mkdir "$LOCK_DIR" 2> /dev/null ; then
|
||||
|
||||
## - Remove lockdir when the script finishes, or when it receives a signal
|
||||
trap clean_up SIGHUP SIGINT SIGTERM
|
||||
|
||||
else
|
||||
|
||||
datum="$(date +"%d.%m.%Y %H:%M")"
|
||||
|
||||
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
|
||||
|
||||
echo ""
|
||||
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
|
||||
echo ""
|
||||
echo -e " Exiting now.."
|
||||
echo ""
|
||||
|
||||
for _email in ${alert_email_arr[@]} ; do
|
||||
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
|
||||
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
|
||||
done
|
||||
|
||||
exit 1
|
||||
|
||||
if [ "X${PIDS}X" = "XX" ];then
|
||||
echo -e "\n[Error]: Samba Service seems to be down. Try to (re)start .."
|
||||
/etc/init.d/samba4 stop > /dev/null 2>&1
|
||||
/etc/init.d/samba4 start > /dev/null
|
||||
sleep 2
|
||||
NEW_PIDS=`ps aux | grep "/usr/local/samba/sbin/samba" | grep -v grep | awk '{print$2}'`
|
||||
if [ "X${NEW_PIDS}X" = "XX" ]; then
|
||||
echo -e ""
|
||||
echo -e "\t[Error]: Restarting samba services failed !!"
|
||||
echo -e "\n\t[Error]: Restarting samba services failed !!\n"
|
||||
echo -e ""
|
||||
else
|
||||
PIDS=""
|
||||
for pid in $NEW_PIDS ; do
|
||||
PIDS="$PIDS $pid"
|
||||
done
|
||||
echo -e
|
||||
echo -e "\tI have restarted the samba services. The new PIDs are $PIDS"
|
||||
echo -e
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
|
||||
# -------------
|
||||
# - Some functions
|
||||
# -------------
|
||||
|
||||
clean_up() {
|
||||
|
||||
# Perform program exit housekeeping
|
||||
rm -rf "$LOCK_DIR"
|
||||
exit $1
|
||||
}
|
||||
|
||||
echononl(){
|
||||
if $terminal ; then
|
||||
echo X\\c > /tmp/shprompt$$
|
||||
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
|
||||
echo -e -n "$*\\c" 1>&2
|
||||
else
|
||||
echo -e -n "$*" 1>&2
|
||||
fi
|
||||
rm /tmp/shprompt$$
|
||||
fi
|
||||
}
|
||||
|
||||
fatal(){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mFatal\033[m ]: $*"
|
||||
echo ""
|
||||
echo -e " \033[31m\033[1mScript was interupted\033[m!"
|
||||
else
|
||||
echo " [ Fatal ]: $*"
|
||||
echo ""
|
||||
echo " Script was terminated...."
|
||||
fi
|
||||
echo ""
|
||||
clean_up 1
|
||||
}
|
||||
|
||||
error (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
|
||||
else
|
||||
echo "[ Error ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
warn (){
|
||||
echo ""
|
||||
if $terminal ; then
|
||||
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
|
||||
else
|
||||
echo "[ Warning ]: $*"
|
||||
fi
|
||||
echo ""
|
||||
}
|
||||
|
||||
info (){
|
||||
if $terminal ; then
|
||||
echo ""
|
||||
echo -e " [ \033[32m\033[1mInfo\033[m ]: $*"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
|
||||
echo_done() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[32mdone\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_failed(){
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
|
||||
fi
|
||||
}
|
||||
echo_skipped() {
|
||||
if $terminal && $LOGGING ; then
|
||||
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# -------------
|
||||
# - Check some prerequisites
|
||||
# -------------
|
||||
|
||||
# - Running in a terminal?
|
||||
# -
|
||||
if [[ -t 1 ]] ; then
|
||||
terminal=true
|
||||
else
|
||||
terminal=false
|
||||
fi
|
||||
|
||||
# - Systemd supported ?
|
||||
# -
|
||||
systemd=$(which systemd)
|
||||
systemctl=$(which systemctl)
|
||||
|
||||
systemd_supported=false
|
||||
if [[ -n "$systemd" ]] && [[ -n "$systemctl" ]] ; then
|
||||
systemd_supported=true
|
||||
fi
|
||||
|
||||
check_string_ps="(sbin/smbd | sbin/samba )"
|
||||
|
||||
# - Check How to start/stop service
|
||||
# -
|
||||
SYSTEMD_UNIT_FILE=""
|
||||
SYSY_INIT_SCRIPT=""
|
||||
if $systemd_supported ; then
|
||||
SYSTEMD_UNIT_FILE="$(systemctl list-unit-files | grep -E "samba[^@]*\.service" | grep "enabled" | cut -d' ' -f1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]]; then
|
||||
SYSY_INIT_SCRIPT="$(service --status-all | awk '{print$4}' | grep samba | head -1)"
|
||||
fi
|
||||
|
||||
if [[ -z "$SYSTEMD_UNIT_FILE" ]] && [[ -z "$SYSY_INIT_SCRIPT" ]] ; then
|
||||
fatal 'Neither an init-script nor a service file for samba service found!'
|
||||
fi
|
||||
|
||||
|
||||
# -------------
|
||||
# - Main Part of Script
|
||||
# -------------
|
||||
|
||||
if $terminal ; then
|
||||
echo -e "\n Check if Samba Service is running.."
|
||||
echo -e " ==================================="
|
||||
fi
|
||||
|
||||
PIDS="$(ps ax | grep -E "$check_string_ps" | grep -v grep | awk '{print$1}')"
|
||||
|
||||
if [[ -z "$PIDS" ]];then
|
||||
|
||||
error "Samba Service seems to be down. Try to (re)start .."
|
||||
|
||||
echononl "Stop Samba Service first .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl stop $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT stop > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
|
||||
echononl "Start Samba Service .."
|
||||
if [[ -n "$SYSTEMD_UNIT_FILE" ]] ; then
|
||||
systemctl start $SYSTEMD_UNIT_FILE > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
else
|
||||
/etc/init.d/$SYSY_INIT_SCRIPT start > /dev/null 2>&1
|
||||
if [[ $? -eq 0 ]] ; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
declare -i count=0
|
||||
|
||||
while [[ $count -lt 10 ]] && [[ -z "${PIDS}" ]]; do
|
||||
sleep 1
|
||||
PIDS="$(ps ax | grep -E "$check_string_ps" | grep -v grep | awk '{print$1}')"
|
||||
((count++))
|
||||
done
|
||||
|
||||
if [[ -z "${PIDS}" ]] ; then
|
||||
error "Restarting Samba Service failed"
|
||||
else
|
||||
|
||||
NEW_PIDS=""
|
||||
for _pid in $NEW_PIDS ; do
|
||||
NEW_PIDS="$PIDS $_pid"
|
||||
done
|
||||
|
||||
info "the new PIDs are $NEW_PIDS"
|
||||
|
||||
echo ""
|
||||
echo " Samba Service was restarted and is now up and running."
|
||||
echo " The new PIDs are $NEW_PIDS"
|
||||
echo ""
|
||||
|
||||
fi
|
||||
|
||||
else
|
||||
info "Samba Service is up and running."
|
||||
fi
|
||||
|
||||
clean_up 0
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user