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.

#6

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-05 10:52:10 -05:00
parent 86be5fd1c1
commit 6125fb9d6b

View File

@@ -18,6 +18,8 @@ LOGFILE="${PROXDOC_LOGFILE:-}"
################### ###################
# Disk list cache - populated on first use # Disk list cache - populated on first use
DISK_LIST="" DISK_LIST=""
# Unit files cache - populated on first use
UNIT_FILES=""
################### ###################
# Pattern Constants # Pattern Constants
@@ -128,6 +130,20 @@ get_disk_list() {
echo "$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 # Efficient process wait with optional spinner
# Usage: wait_for_process $pid [delay] # Usage: wait_for_process $pid [delay]
# Uses kill -0 instead of ps -p for efficiency # Uses kill -0 instead of ps -p for efficiency
@@ -531,7 +547,7 @@ get_node_exporter_status() {
else else
log_message warn "Port 9100 not listening" log_message warn "Port 9100 not listening"
fi 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" log_message warn "Node Exporter is installed but not running"
echo -e "Start with: systemctl start node_exporter" echo -e "Start with: systemctl start node_exporter"
else else
@@ -546,7 +562,7 @@ get_hwmon_status() {
systemctl list-timers hwmon.timer --no-pager 2>/dev/null systemctl list-timers hwmon.timer --no-pager 2>/dev/null
echo -e "\n${GREEN}Last Run:${NC}" echo -e "\n${GREEN}Last Run:${NC}"
journalctl -u hwmon.service -n 3 --no-pager 2>/dev/null || true 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" log_message warn "hwmon timer is installed but not active"
echo -e "Enable with: systemctl enable --now hwmon.timer" echo -e "Enable with: systemctl enable --now hwmon.timer"
else else