From ae9b50da3618abfaf60f4a90f0287e053566a80a Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Fri, 8 Jul 2016 15:25:28 +0100 Subject: [PATCH 1/6] rc-status --- efunctions/rc-status | 85 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 efunctions/rc-status diff --git a/efunctions/rc-status b/efunctions/rc-status new file mode 100755 index 0000000..43e9aca --- /dev/null +++ b/efunctions/rc-status @@ -0,0 +1,85 @@ +#!/bin/sh +# Copyright (c) 2016 Marcus Downing +# Released under the 2-clause BSD license. + +. ../functions.sh +. efunctions_ecolors + +command_exists() { + command -v "$1" >/dev/null 2>/dev/null +} + +# cf +map_put () { + alias "hash__${1}$2"="$3" +} + +map_get() { + alias "hash__${1}$2" 2>/dev/null | awk -F"'" '{ print $2; }' +} + +map_keys() { + alias | grep '^hash__'$1 | cut -d'=' -f1 | awk -F"$1" '{print $2; }' +} + + +# System V jobs +if command_exists service; then + for service in $(service --status-all 2>&1 | cut -c 9-); do + map_put services "$service" + map_put is_sysv "$service" yes + + if service "$service" status >/dev/null 2>&1; then + map_put running "$service" "yes" + else + map_put running "$service" "no" + fi + done +fi + +# Upstart jobs +if command_exists initctl; then + for service in `initctl list | cut -d ' ' -f 1`; do + map_put services "$service" + map_put is_upstart "$service" yes + map_put status "$service" "no" + + if initctl status "$service" 2>&1 | grep "start/running" >/dev/null; then + map_put running "$service" "yes" + else + map_put running "$service" "no" + fi + done +fi + +# TODO systemd? + +# Output +bold=`tput bold` + +all_services="$(map_keys services | sort | uniq)" +for service in $all_services; do + running="$(map_get running "$service")" + + src="" + if [ "$(map_get is_upstart "$service")" = "yes" ]; then + src=" ${BRACKET}(upstart)" + else + if [ "$(map_get is_sysv "$service")" = "yes" ]; then + src=" ${BRACKET}(system V)" + fi + fi + + if [ "yes" = "$running" ]; then + einfo "$bold$service$src" + eend 0 + else + if [ "no" = "$running" ]; then + eerror "$service$src" + eend 1 + else + ewarn "$service$src" + eend 1 + fi + fi +done From 9da2d5d6caa657d46d3cc93621fff27ec36123c4 Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Fri, 8 Jul 2016 15:31:11 +0100 Subject: [PATCH 2/6] Formatting --- efunctions/rc-status | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/efunctions/rc-status b/efunctions/rc-status index 43e9aca..4f1dd6d 100755 --- a/efunctions/rc-status +++ b/efunctions/rc-status @@ -61,24 +61,24 @@ all_services="$(map_keys services | sort | uniq)" for service in $all_services; do running="$(map_get running "$service")" - src="" + service_name="$(printf "%-40s" "$service")" + src="${BRACKET}" if [ "$(map_get is_upstart "$service")" = "yes" ]; then - src=" ${BRACKET}(upstart)" - else - if [ "$(map_get is_sysv "$service")" = "yes" ]; then - src=" ${BRACKET}(system V)" - fi + src="$src upstart" + fi + if [ "$(map_get is_sysv "$service")" = "yes" ]; then + src="$src system V" fi if [ "yes" = "$running" ]; then - einfo "$bold$service$src" + einfo "$bold$service_name$src" eend 0 else if [ "no" = "$running" ]; then - eerror "$service$src" + eerror "$service_name$src" eend 1 else - ewarn "$service$src" + ewarn "$service_name$src" eend 1 fi fi From dfb4c7a61f2a3a15c0a5b47610372e4385c39472 Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Fri, 8 Jul 2016 16:23:30 +0100 Subject: [PATCH 3/6] Table formatting --- efunctions/rc-status | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/efunctions/rc-status b/efunctions/rc-status index 4f1dd6d..cf41423 100755 --- a/efunctions/rc-status +++ b/efunctions/rc-status @@ -31,8 +31,8 @@ if command_exists service; then if service "$service" status >/dev/null 2>&1; then map_put running "$service" "yes" - else - map_put running "$service" "no" + # else + # map_put running "$service" "no" fi done fi @@ -46,8 +46,13 @@ if command_exists initctl; then if initctl status "$service" 2>&1 | grep "start/running" >/dev/null; then map_put running "$service" "yes" - else - map_put running "$service" "no" + + pid="$(initctl status "$service" | grep -o 'process [0-9]\+' | sed 's/^process //')" + if [ ! -z "$pid" ]; then + map_put pid "$service" "$pid" + fi + # else + # map_put running "$service" "no" fi done fi @@ -65,21 +70,24 @@ for service in $all_services; do src="${BRACKET}" if [ "$(map_get is_upstart "$service")" = "yes" ]; then src="$src upstart" + else + src="$src " fi if [ "$(map_get is_sysv "$service")" = "yes" ]; then - src="$src system V" + src="$src sysV" + else + src="$src " fi + pid="$NORMAL $(printf "%6s" "$(map_get pid "$service")")" + if [ "yes" = "$running" ]; then - einfo "$bold$service_name$src" - eend 0 + einfo "$bold$service_name$src$pid" else - if [ "no" = "$running" ]; then - eerror "$service_name$src" - eend 1 + if [ "no" = "$running" -o "" = "$running" ]; then + eerror "$service_name$src$pid" else - ewarn "$service_name$src" - eend 1 + ewarn "$service_name$src$pid" fi fi done From 1746c4e64190624ff75622b1d876361b43e03f01 Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Fri, 8 Jul 2016 16:56:18 +0100 Subject: [PATCH 4/6] Moved rc-status --- efunctions/rc-status => rc-status | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename efunctions/rc-status => rc-status (98%) diff --git a/efunctions/rc-status b/rc-status similarity index 98% rename from efunctions/rc-status rename to rc-status index cf41423..dd63934 100755 --- a/efunctions/rc-status +++ b/rc-status @@ -2,7 +2,7 @@ # Copyright (c) 2016 Marcus Downing # Released under the 2-clause BSD license. -. ../functions.sh +. "$(dirname "$0")"/functions.sh . efunctions_ecolors command_exists() { From 56fb01d5bbdd49549e4ef8fa68b6f9284afdf37f Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Fri, 8 Jul 2016 17:18:27 +0100 Subject: [PATCH 5/6] sub-services --- rc-status | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/rc-status b/rc-status index dd63934..c912141 100755 --- a/rc-status +++ b/rc-status @@ -55,8 +55,26 @@ if command_exists initctl; then # map_put running "$service" "no" fi done -fi + IFS=' +' + for service in `initctl list | grep '(.*)'`; do + base="$(echo "$service" | cut -d ' ' -f 1)" + unit="$(echo "$service" | cut -d ' ' -f 2)" + unit="${unit#(}" + unit="${unit%)}" + + map_put has_units "$base" yes + map_put service_units "$base" "$(map_get service_units "$base") $unit" + + if echo "$service" | grep "start/running" >/dev/null; then + map_put running "${base}__${unit}" yes + fi + # einfo "sub service $base -> $unit" + done + unset IFS +fi +# echo "\n\n" # TODO systemd? # Output @@ -81,13 +99,29 @@ for service in $all_services; do pid="$NORMAL $(printf "%6s" "$(map_get pid "$service")")" - if [ "yes" = "$running" ]; then - einfo "$bold$service_name$src$pid" + if [ "$(map_get has_units "$service")" ]; then + einfo "$service_name" + eindent + + for unit in `map_get service_units "$service" | tr ' ' '\n' | sort`; do + unit_running="$(map_get running "${service}__${unit}")" + if [ "yes" = "$unit_running" ]; then + einfo "$unit" + else + eerror "$unit" + fi + done + + eoutdent else - if [ "no" = "$running" -o "" = "$running" ]; then - eerror "$service_name$src$pid" + if [ "yes" = "$running" ]; then + einfo "$bold$service_name$src$pid" else - ewarn "$service_name$src$pid" + if [ "no" = "$running" -o "" = "$running" ]; then + eerror "$service_name$src$pid" + else + ewarn "$service_name$src$pid" + fi fi fi done From e09c91acd56717a00ad3175b5db43e4bf51dc29d Mon Sep 17 00:00:00 2001 From: Marcus Downing Date: Tue, 12 Jul 2016 16:25:43 +0100 Subject: [PATCH 6/6] Colours for system flags --- rc-status | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rc-status b/rc-status index c912141..7fa0c23 100755 --- a/rc-status +++ b/rc-status @@ -85,7 +85,10 @@ for service in $all_services; do running="$(map_get running "$service")" service_name="$(printf "%-40s" "$service")" - src="${BRACKET}" + src="" + if [ "yes" = "$running" ]; then + src="${BRACKET}" + fi if [ "$(map_get is_upstart "$service")" = "yes" ]; then src="$src upstart" else @@ -98,6 +101,9 @@ for service in $all_services; do fi pid="$NORMAL $(printf "%6s" "$(map_get pid "$service")")" + # if [ -z "$pid" ]; then + + # fi if [ "$(map_get has_units "$service")" ]; then einfo "$service_name"