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) }'
}
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() {
if [ -S $1 ]; then
true
@ -195,6 +201,23 @@ print_scrollback() {
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)
mode=$1
shift
@ -248,10 +271,10 @@ case $mode in
;;
"log")
case "$1" in
"on"|"off");;
"on"|"off"|"status"|"reset");;
*)
echo >&2 "'$(basename $0) log' accepts:"
echo >&2 " on, off"
echo >&2 " on, off,status,reset"
exit $EXIT_INVALID_SUBMODE
;;
esac
@ -281,16 +304,25 @@ case $mode in
fi
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
exit $EXIT_SUCCESS
;;
"terminate"|"term"|"t"|"kill"|"k")
# Check if we are in terminate/kill mode
if [[ ${mode::1} == "k" ]]; then
SIGNAL="-SIGKILL"
else
SIGNAL="-SIGTERM"
fi
# If no session name specified: terminate all sessions
# If no session name specified: kill all sessions
if [ -z "$1" ]; then
read -p "Terminate all sessions? [y/N] " -n 1 -r
printf "\n"
@ -307,20 +339,35 @@ case $mode in
exit_if_nonexisting_session $session_name
fi
for sname in ${session_name[@]}; do
echo $sname
PID=$(get_session_pid $sname)
session_path=$(get_session_path $sname)
log_path=$(get_log_path $sname)
fifo_path=$(get_fifo_path $sname)
[[ -a $fifo_path ]] && echo > $fifo_path
[[ ! -z "$PID" ]] && kill $SIGNAL $PID
tail --pid=$PID -f /dev/null
[[ -a $session_path ]] && rm -f $session_path
[[ -a $fifo_path ]] && rm -f $fifo_path
[[ -f $log_path ]] && rm -f $log_path
# Turn logging off if it is activated
if is_logging $sname; then
session log off $sname
fi
# Get pid of main process within session
subpid=$(get_session_subpid $sname) # main process
abdpid=$(get_session_pid $sname) # abduco
# kill it
if [[ ${mode::1} == "k" ]]; then
kill -SIGKILL $subpid
else
kill_recursively $subpid SIGTERM true
fi
kill -SIGTERM $abdpid
done
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")
sessions_avail=($(get_available_sessions))
for sname in ${sessions_avail[@]}; do
@ -339,12 +386,10 @@ case $mode in
"quit"|"q")
exit_if_not_in_session
session_name=$ABDUCO_SESSION
CMD=""
if is_logging $session_name; then
CMD+="session log off && "
session log off
fi
CMD+="exit"
echo "$CMD" | abduco -p $session_name
echo "exit" | abduco -p $session_name
exit $EXIT_SUCCESS
;;
"help"|"h")