-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-services.sh
executable file
·47 lines (41 loc) · 1.19 KB
/
get-services.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
status="all" # Default status (all services)
print_services_json() {
local status="$1"
local services_list
if [[ "$status" == "all" ]]; then
services_list=$(systemctl list-units --type=service --all --no-pager --plain --no-legend)
else
services_list=$(systemctl list-units --type=service --state="$status" --no-pager --plain --no-legend)
fi
local json_output='{"services": ['
local first=true
while read -r unit load active sub description; do
if $first; then
first=false
else
json_output+=","
fi
json_output+="\n{\"unit\": \"$unit\", \"load\": \"$load\", \"active\": \"$active\", \"sub\": \"$sub\", \"description\": \"$description\"}"
done <<< "$services_list"
json_output+="\n]}"
echo -e "$json_output" | jq .
}
if [[ $# -eq 1 ]]; then
case "$1" in
running)
status="running"
;;
exited)
status="exited"
;;
all)
status="all"
;;
*)
echo "Invalid status option. Usage: $0 [running|exited|all]"
exit 1
;;
esac
fi
print_services_json "$status"