راه اندازی مجدد سرویسها بعد از بروزرسانی

مدتی بود دنبال راه حلی می گشتم تا بعد از به روز رسانی در صورتی که سیستم نیاز به restart داره متوجه بشم.

مدتی بود با بسته ای به نام debian-goodies آشنا شده بودم که مجموعه ای از دستورات کاربردی بود. یکی از این دستورات checkrestart هست که سرویس‌هایی که باید restart شوند و بسته‌هایی که سرویسی از آنها استفاده نمی کند که restart سرویس مشکل را حل کند را لیست می کند.

این دستور تنها گزارش می‌دهد و خود اقدام به راه اندازی مجدد سرویس ها و سیستم نمی کند. برای این منظور (یعنی راه اندازی خودکار سرویس‌ها) در نسخه جدید بسته اسکریپتی به نام restart-services معرفی شده است که هنوز با مشکل اجرا می گردد و عملا کار نمی کند.

من اومدم این اسکریپت را برای خودم اصلاح کردم تا ازش استفاده کنم. البته اسکریپت خیلی فکر شده و ترو تمیزی نیست ولی کار می کنه . سرویس های مورد نیاز را restart می کنه و در صورت نیاز به راه اندازی مجدد سیستم عامل برای مدیر سیستم ایمیلی ارسال می کند.

#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA
#
# On Debian systems, a copy of the GNU General Public License may be
# found in /usr/share/common-licenses/GPL.
#
# Sysadmin email address
SYSADM=”behrad_es@yahoo.com”

function help()
{
cat<<-EOH
$0 [-h|–help] [-s]
Try to restart services, as indicated by checkrestart.

Options:
-h, –help … help
-s, –simulate … simulate, do nothing, just print actions

Return codes:
0 … OK
1 … error
2 … warning
EOH

exit 0
}

function simulate ()
{
echo “This/These service(s) need restart:”
checkrestart  2>/dev/null | sed -n ‘/^These are the init scripts:/,/^$/ p’ | grep init.d

echo “”
echo “These files need restart server to upgraded”
checkrestart  2>/dev/null | sed -n ‘/^These processes do not seem to have an associated init script to restart them:/,/^$/ p’ | grep “/.*”

exit 0
}

# arguments parsing
while [ “$#” != “0” ]; do
case “$1” in
-s|–simulate)
simulate
;;
-h|–help)
help
;;
esac
shift
done

# Restart serivices after update
OIFS=$IFS
IFS=”

checkrestart  2>/dev/null | sed -n ‘/^These are the init scripts:/,/^$/ p’ | grep init.d | mail -s “Services restarted” $SYSADM
for i in `checkrestart  2>/dev/null | sed -n ‘/^These are the init scripts:/,/^$/ p’  | grep init.d`
do
eval $i
done
IFS=$OIFS

# Find when restart required
if [ “`checkrestart  2>/dev/null | grep “These processes do not seem to have an associated init script to restart them:”`” != “” ]
then
echo “New upgrade need restart. Plan to restart server.”
echo “New upgrade need restart. Plan to restart server.” | mail -s “Schedule restart” $SYSADM
fi

exit 0

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *