From 6125fb9d6be259d653cef50ea6a0d8f565398948 Mon Sep 17 00:00:00 2001 From: Jared Vititoe Date: Thu, 5 Feb 2026 10:52:10 -0500 Subject: [PATCH] Cache systemctl list-unit-files to avoid repeated calls Add get_unit_files() and unit_file_exists() helper functions that cache the output of systemctl list-unit-files. This avoids running the same command multiple times when checking for node_exporter and hwmon.timer unit files. https://code.lotusguild.org/LotusGuild/proxDoc/issues/6 Co-Authored-By: Claude Opus 4.5 --- proxDoc.sh | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/proxDoc.sh b/proxDoc.sh index c578442..6d3d8a4 100755 --- a/proxDoc.sh +++ b/proxDoc.sh @@ -18,6 +18,8 @@ LOGFILE="${PROXDOC_LOGFILE:-}" ################### # Disk list cache - populated on first use DISK_LIST="" +# Unit files cache - populated on first use +UNIT_FILES="" ################### # Pattern Constants @@ -128,6 +130,20 @@ get_disk_list() { echo "$DISK_LIST" } +# Get systemctl unit files with caching +get_unit_files() { + if [[ -z "$UNIT_FILES" ]]; then + UNIT_FILES=$(systemctl list-unit-files 2>/dev/null) + fi + echo "$UNIT_FILES" +} + +# Check if a unit file exists (uses cached data) +unit_file_exists() { + local unit_name="$1" + get_unit_files | grep -q "$unit_name" +} + # Efficient process wait with optional spinner # Usage: wait_for_process $pid [delay] # Uses kill -0 instead of ps -p for efficiency @@ -531,7 +547,7 @@ get_node_exporter_status() { else log_message warn "Port 9100 not listening" fi - elif systemctl list-unit-files 2>/dev/null | grep -q node_exporter; then + elif unit_file_exists node_exporter; then log_message warn "Node Exporter is installed but not running" echo -e "Start with: systemctl start node_exporter" else @@ -546,7 +562,7 @@ get_hwmon_status() { systemctl list-timers hwmon.timer --no-pager 2>/dev/null echo -e "\n${GREEN}Last Run:${NC}" journalctl -u hwmon.service -n 3 --no-pager 2>/dev/null || true - elif systemctl list-unit-files 2>/dev/null | grep -q hwmon.timer; then + elif unit_file_exists hwmon.timer; then log_message warn "hwmon timer is installed but not active" echo -e "Enable with: systemctl enable --now hwmon.timer" else