Skip to content

Commit

Permalink
Add a configurable "ignored service" list to checkservices
Browse files Browse the repository at this point in the history
This commit aims to introduce an ignored service list that contains services that are known to cause parsing errors and/or issues when being restarted. Elements can be added to the list with the `-i "service_name".service` option (e.g. `checkservices -i sddm.service NetworkManager.service`)

Fixes #44
  • Loading branch information
Antiz96 committed Jul 15, 2024
1 parent c53a022 commit ccf9f26
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions admin/checkservices
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ SERIALIZE=0 # run in parallel
STATUS=1 # display status after systemctl
USER_SLICE=0 # act on users services

# ignored service list
IGNORED_SERVICES=("getty@tty.*.service" "systemd-logind.service" "dbus-broker.service")

# print $* as an arrow line
arrow() {
printf "${C_BOLD}${C_BLUE}:: ${C_WHITE}%s${C_RESET}\n" "$*"
Expand Down Expand Up @@ -102,7 +105,7 @@ confirm() {

# get running systemd services
get_services() {
systemctl --no-legend --full --type service --state running | tr -d '' | awk '{print $1}'
systemctl --no-legend --full --type service --state running | tr -d '' | awk '{print $1}' | grep -v $(printf -- '-e %s ' "${IGNORED_SERVICES[@]}")
}

# get systemd services with updated mapped files
Expand Down Expand Up @@ -249,14 +252,15 @@ usage() {
echo " -s/-S: display (or not) status of restarted service (default: $STATUS)" >&2
echo " -u/-U: act (or not) on services in users slice (default: $USER_SLICE)" >&2
echo " -z/-Z: serialize (or not) action (default: $SERIALIZE)" >&2
echo " -i 'service_name'.service: list of services to ignore (space separated)" >&2
exit 2
}

# parse command line arguments
# set options as global vars
argparse() {
local opt
while getopts 'AahFfLlPpRrSsUuZz' opt; do
while getopts 'AahFfLlPpRrSsUuZzi' opt; do
case $opt in
A) AUTOCONFIRM=0;; a) AUTOCONFIRM=1;;
F) FAILED=0;; f) FAILED=1;;
Expand All @@ -266,6 +270,13 @@ argparse() {
S) STATUS=0;; s) STATUS=1;;
U) USER_SLICE=0;; u) USER_SLICE=1;;
Z) SERIALIZE=0;; z) SERIALIZE=1;;
i) shift $((OPTIND-1))
while [[ "$1" != "" && "$1" != -* && "$1" == *.service ]]; do
IGNORED_SERVICES+=("$1")
shift
done
OPTIND=1
;;

This comment has been minimized.

Copy link
@lahwaacz

lahwaacz Jul 15, 2024

Contributor

This handling is not very getopts-like and does not work e.g. if the user runs checkservices -ia...

There is a simple way to handle an option with a required value and it can be used multiple times on the command line, see https://stackoverflow.com/a/20761893

The example usage would be checkservices -i sddm.service -i NetworkManager.service

This comment has been minimized.

Copy link
@Antiz96

Antiz96 Jul 16, 2024

Author Contributor

Thanks for the pointer, fixed!

*) usage;;
esac
done
Expand Down

0 comments on commit ccf9f26

Please sign in to comment.