improved terminate/kill; added status/reset submode to log; improved quit

This commit is contained in:
Michael Krayer 2021-04-20 14:41:20 +02:00
parent 41e3baa8ec
commit c8680334fa
1 changed files with 69 additions and 24 deletions

93
session
View File

@ -114,6 +114,12 @@ get_session_pid() {
abduco | tail +2 | grep $session_name | awk '{ print $(NF-1) }' abduco | tail +2 | grep $session_name | awk '{ print $(NF-1) }'
} }
get_session_subpid() {
local session_name=$1
local ppid=$(get_session_pid $session_name)
ps --ppid=$ppid | awk 'NR==2 {print $1; exit}'
}
is_existing_session_path() { is_existing_session_path() {
if [ -S $1 ]; then if [ -S $1 ]; then
true true
@ -195,6 +201,23 @@ print_scrollback() {
tail -n ${config[scrollback]} $log_path tail -n ${config[scrollback]} $log_path
} }
kill_recursively() {
local pid="$1"
local signal="$2"
local and_self="${3:-false}"
if children="$(pgrep -P "$pid")"; then
for child in $children; do
kill_recursively "$child" "$signal" true
done
fi
if [[ "$and_self" == true ]]; then
kill -$signal "$pid"
while kill -0 "$pid" 2>/dev/null;do
sleep 0.01;
done
fi
}
## Parse first argument (= mode argument) ## Parse first argument (= mode argument)
mode=$1 mode=$1
shift shift
@ -248,10 +271,10 @@ case $mode in
;; ;;
"log") "log")
case "$1" in case "$1" in
"on"|"off");; "on"|"off"|"status"|"reset");;
*) *)
echo >&2 "'$(basename $0) log' accepts:" echo >&2 "'$(basename $0) log' accepts:"
echo >&2 " on, off" echo >&2 " on, off,status,reset"
exit $EXIT_INVALID_SUBMODE exit $EXIT_INVALID_SUBMODE
;; ;;
esac esac
@ -281,16 +304,25 @@ case $mode in
fi fi
echo > $fifo_path echo > $fifo_path
;; ;;
"status")
if is_logging $session_name; then
echo "Logging is activated.";
else
echo "Logging is not activated.";
exit $EXIT_NOT_LOGGING
fi
;;
"reset")
if is_logging $session_name; then
session log off $session_name
fi
session log on $session_name
;;
esac esac
exit $EXIT_SUCCESS
;; ;;
"terminate"|"term"|"t"|"kill"|"k") "terminate"|"term"|"t"|"kill"|"k")
# Check if we are in terminate/kill mode # If no session name specified: kill all sessions
if [[ ${mode::1} == "k" ]]; then
SIGNAL="-SIGKILL"
else
SIGNAL="-SIGTERM"
fi
# If no session name specified: terminate all sessions
if [ -z "$1" ]; then if [ -z "$1" ]; then
read -p "Terminate all sessions? [y/N] " -n 1 -r read -p "Terminate all sessions? [y/N] " -n 1 -r
printf "\n" printf "\n"
@ -307,20 +339,35 @@ case $mode in
exit_if_nonexisting_session $session_name exit_if_nonexisting_session $session_name
fi fi
for sname in ${session_name[@]}; do for sname in ${session_name[@]}; do
echo $sname # Turn logging off if it is activated
PID=$(get_session_pid $sname) if is_logging $sname; then
session_path=$(get_session_path $sname) session log off $sname
log_path=$(get_log_path $sname) fi
fifo_path=$(get_fifo_path $sname) # Get pid of main process within session
[[ -a $fifo_path ]] && echo > $fifo_path subpid=$(get_session_subpid $sname) # main process
[[ ! -z "$PID" ]] && kill $SIGNAL $PID abdpid=$(get_session_pid $sname) # abduco
tail --pid=$PID -f /dev/null # kill it
[[ -a $session_path ]] && rm -f $session_path if [[ ${mode::1} == "k" ]]; then
[[ -a $fifo_path ]] && rm -f $fifo_path kill -SIGKILL $subpid
[[ -f $log_path ]] && rm -f $log_path else
kill_recursively $subpid SIGTERM true
fi
kill -SIGTERM $abdpid
done done
exit $EXIT_SUCCESS exit $EXIT_SUCCESS
;; ;;
"processes"|"ps")
if [ -z "$1" ]; then
exit_if_not_in_session
session_name=$ABDUCO_SESSION
else
session_name=$1
exit_if_nonexisting_session $session_name
fi
pid=$(get_session_pid $session_name)
pstree -g "$pid"
exit $EXIT_SUCCESS
;;
"list"|"ls"|"l") "list"|"ls"|"l")
sessions_avail=($(get_available_sessions)) sessions_avail=($(get_available_sessions))
for sname in ${sessions_avail[@]}; do for sname in ${sessions_avail[@]}; do
@ -339,12 +386,10 @@ case $mode in
"quit"|"q") "quit"|"q")
exit_if_not_in_session exit_if_not_in_session
session_name=$ABDUCO_SESSION session_name=$ABDUCO_SESSION
CMD=""
if is_logging $session_name; then if is_logging $session_name; then
CMD+="session log off && " session log off
fi fi
CMD+="exit" echo "exit" | abduco -p $session_name
echo "$CMD" | abduco -p $session_name
exit $EXIT_SUCCESS exit $EXIT_SUCCESS
;; ;;
"help"|"h") "help"|"h")