Skip to content
checkservices 2.37 KiB
Newer Older
Seblu's avatar
Seblu committed
#!/bin/bash
Seblu's avatar
Seblu committed

Seblu's avatar
Seblu committed
# Copyright © Sébastien Luttringer
Seblu's avatar
Seblu committed
#
# 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
# of the License, 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.

# Check running systemd services for binary update
# Convenient way to restart updated systemd service after upgrade
Seblu's avatar
Seblu committed

(( $UID != 0 )) && echo 'You need to be root' && exit 1

Seblu's avatar
Seblu committed
if [[ -t 1 ]]; then
	shopt -s xpg_echo
	c_title='\e[1;33m'
	c_svc='\e[1;31m'
	c_rst='\e[m'
fi

Seblu's avatar
Seblu committed
usage() {
	echo "usage ${0##*/} [options]"
	echo 'options:'
	echo '   -h: this help' >&2
	echo '   -r: restart services' >&2
Seblu's avatar
Seblu committed
	echo '   -l: list systemd commands to execute' >&2
Seblu's avatar
Seblu committed
	echo '   -R: reload services' >&2
	echo "   -n: don't call systemd daemon-reload" >&2
Seblu's avatar
Seblu committed
	echo "   -v: verbose mode" >&2
Seblu's avatar
Seblu committed
	exit 1
}

Seblu's avatar
Seblu committed
while getopts 'hrlRnv' opt; do
Seblu's avatar
Seblu committed
	case $opt in
		r) systemd_cmd='restart';;
		R) systemd_cmd='reload';;
		n) daemon_reload=false;;
		l) list=true;;
Seblu's avatar
Seblu committed
		v) verbose=true;;
Seblu's avatar
Seblu committed
		*) usage;;
	esac
done
shift $((OPTIND - 1));
(( $# > 0 )) && usage

[[ -n "$systemd_cmd" || -n "$list" ]] || usage

[[ $daemon_reload ]] || systemctl --system daemon-reload

Seblu's avatar
Seblu committed
declare -a services
services=($(systemctl -t service --full|grep \.service|grep running|sed -rn 's/^(.*\.service).*/\1/p'))

for svc in "${services[@]}"; do
	pid=$(systemctl show "$svc"|sed -nr 's/^MainPID=(.*)/\1/p')
	deleted=$(grep '(deleted)' "/proc/$pid/maps"|sed -nr 's|^\S+ ..x. \S+ \S+ \S+ \s+||p'|sort|uniq)
	if [[ -n $deleted ]]; then
Seblu's avatar
Seblu committed
		if [[ $verbose ]]; then
Seblu's avatar
Seblu committed
			echo "${c_title}Service:${c_svc} $svc${c_rst}"
			echo "${c_title}Pid:${c_rst} $pid"
			echo "${c_title}Commands:${c_rst}"
			echo "systemctl reload '$svc'"
			echo "systemctl restart '$svc'"
			echo "${c_title}Deleted files:${c_rst}"
Seblu's avatar
Seblu committed
			echo "$deleted"
			echo
		else
			echo "systemctl restart '$svc'"
		fi
		[[ $systemd_cmd ]] && { systemctl "$systemd_cmd" "$svc" & }
Seblu's avatar
Seblu committed
	fi
done