Another experiment
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
parent
c6bba36f94
commit
b67fec6f16
56
ssm
56
ssm
|
@ -5,6 +5,26 @@ shopt -s nullglob
|
||||||
is_function() [[ "$(type -t "$1" 2>/dev/null)" == 'function' ]]
|
is_function() [[ "$(type -t "$1" 2>/dev/null)" == 'function' ]]
|
||||||
readonly -f is_function
|
readonly -f is_function
|
||||||
|
|
||||||
|
command_not_found_handle() {
|
||||||
|
declare f=$1; shift; declare argv=( "$@" )
|
||||||
|
|
||||||
|
# Skip the special name 'source'.
|
||||||
|
# Don't call your functions 'source'.
|
||||||
|
declare i
|
||||||
|
for i in "${FUNCNAME[@]:1}"; do
|
||||||
|
[[ $i == 'source' ]] && continue
|
||||||
|
f="${i}${f}"
|
||||||
|
break
|
||||||
|
done
|
||||||
|
|
||||||
|
is_function "$f" || {
|
||||||
|
printf '%s: line %s: %s: command not found\n' "$0" "${BASH_LINENO[0]}" "$f" >&2
|
||||||
|
return 127
|
||||||
|
}
|
||||||
|
|
||||||
|
"$f" "${argv[@]}"
|
||||||
|
}; readonly command_not_found_handle;
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<- EOF
|
cat <<- EOF
|
||||||
Usage: ssm <service> <function>
|
Usage: ssm <service> <function>
|
||||||
|
@ -156,12 +176,14 @@ cgroup_get_procs() {
|
||||||
}; readonly -f cgroup_get_procs
|
}; readonly -f cgroup_get_procs
|
||||||
|
|
||||||
## Run the command and wait for it to die
|
## Run the command and wait for it to die
|
||||||
|
|
||||||
svc() {
|
svc() {
|
||||||
declare job_pid job_exit job_success last_respawn fail_counter date counter p
|
declare job_pid job_exit job_success last_respawn fail_counter date counter p
|
||||||
var job_pid job_exit job_success last_respawn fail_counter date counter p
|
var job_pid job_exit job_success last_respawn fail_counter date counter p
|
||||||
|
|
||||||
svc::cleanup() {
|
svc::cleanup() {
|
||||||
nullexec kill -n "$service_stop_signal" "$job_pid"
|
#nullexec kill -n "$service_stop_signal" "$job_pid"
|
||||||
|
nullexec "${service_stop_command[@]}"
|
||||||
|
|
||||||
anywait "$job_pid" "$service_stop_timeout"
|
anywait "$job_pid" "$service_stop_timeout"
|
||||||
|
|
||||||
|
@ -285,7 +307,7 @@ svc() {
|
||||||
}; readonly -f svc
|
}; readonly -f svc
|
||||||
|
|
||||||
## Run a command with its output discarded
|
## Run a command with its output discarded
|
||||||
nullexec() { "$@" &>/dev/null; }
|
nullexec() { eval "$@" &>/dev/null; }
|
||||||
readonly -f nullexec
|
readonly -f nullexec
|
||||||
|
|
||||||
## Wait for a pid, indefinitely
|
## Wait for a pid, indefinitely
|
||||||
|
@ -490,18 +512,17 @@ stop() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info.item() { printf "%16s: %s\n" "$1" "$2"; }
|
||||||
info() {
|
info() {
|
||||||
declare -a _info_items
|
declare -a _info_items
|
||||||
declare _status_code _status _show_pid
|
declare _status_code _status _show_pid
|
||||||
var _info_items _status_code _status _show_pid
|
var _info_items _status_code _status _show_pid
|
||||||
|
|
||||||
info::item() { printf "%16s: %s\n" "$1" "$2"; }
|
.item Name "$service_name"
|
||||||
|
|
||||||
info::item Name "$service_name"
|
|
||||||
|
|
||||||
status; _status_code = "$?"
|
status; _status_code = "$?"
|
||||||
if service_oneshot; then
|
if service_oneshot; then
|
||||||
info::item Oneshot 'yes'
|
.item Oneshot 'yes'
|
||||||
|
|
||||||
case $_status_code in
|
case $_status_code in
|
||||||
(0) _status = 'success';;
|
(0) _status = 'success';;
|
||||||
|
@ -510,7 +531,7 @@ info() {
|
||||||
(*) _status = 'unknown';;
|
(*) _status = 'unknown';;
|
||||||
esac
|
esac
|
||||||
else
|
else
|
||||||
info::item Restart "$service_respawn"
|
.item Restart "$service_respawn"
|
||||||
|
|
||||||
case $_status_code in
|
case $_status_code in
|
||||||
(0) _status = 'running';;
|
(0) _status = 'running';;
|
||||||
|
@ -522,23 +543,23 @@ info() {
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
|
|
||||||
info::item Status "$_status"
|
.item Status "$_status"
|
||||||
|
|
||||||
info::item Exec "${service_command[*]}"
|
.item Exec "${service_command[*]}"
|
||||||
info::item Config "$service_config"
|
.item Config "$service_config"
|
||||||
info::item Workdir "$service_workdir"
|
.item Workdir "$service_workdir"
|
||||||
|
|
||||||
info::item 'Output log' "$service_logfile_out"
|
.item 'Output log' "$service_logfile_out"
|
||||||
service_logfile_out == "$service_logfile_err" || \
|
service_logfile_out == "$service_logfile_err" || \
|
||||||
info::item 'Error log' "$service_logfile_err"
|
.item 'Error log' "$service_logfile_err"
|
||||||
|
|
||||||
if service_running; then
|
if service_running; then
|
||||||
info::item PID "$service_pid"
|
.item PID "$service_pid"
|
||||||
info::item PIDFile "$service_pidfile"
|
.item PIDFile "$service_pidfile"
|
||||||
|
|
||||||
cgroups && {
|
cgroups && {
|
||||||
info::item Cgroup "$cgroup_home/$service_cgroup_name"
|
.item Cgroup "$cgroup_home/$service_cgroup_name"
|
||||||
info::item 'Cgroup procs' "${#service_cgroup_procs[@]}"
|
.item 'Cgroup procs' "${#service_cgroup_procs[@]}"
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -637,6 +658,7 @@ var XDG_RUNTIME_DIR := "/run/user/$UID"
|
||||||
var service_respawn = 'no' # Respawn the service if it exits
|
var service_respawn = 'no' # Respawn the service if it exits
|
||||||
var service_workdir = '/'
|
var service_workdir = '/'
|
||||||
var service_stop_timeout = 30
|
var service_stop_timeout = 30
|
||||||
|
var service_stop_command = kill -n "\$service_stop_signal" "\$job_pid"
|
||||||
var service_ready_timeout = 15
|
var service_ready_timeout = 15
|
||||||
var service_signals = 1 10 12
|
var service_signals = 1 10 12
|
||||||
var service_reload_signal = 1
|
var service_reload_signal = 1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user