Compare commits

...

2 Commits

1 changed files with 77 additions and 19 deletions

96
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
@ -220,6 +243,7 @@ case $mode in
echo "[attached to '$session_name']" echo "[attached to '$session_name']"
is_logging $session_name && print_scrollback $session_name is_logging $session_name && print_scrollback $session_name
eval $CMD eval $CMD
echo "[detached from '$session_name']"
exit $EXITSUCCESS exit $EXITSUCCESS
;; ;;
"new"|"n") "new"|"n")
@ -247,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
@ -280,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"
@ -306,19 +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
PID=$(get_session_pid $sname) # Turn logging off if it is activated
session_path=$(get_session_path $sname) if is_logging $sname; then
log_path=$(get_log_path $sname) session log off $sname
fifo_path=$(get_fifo_path $sname) fi
[[ -a $fifo_path ]] && echo > $fifo_path # Get pid of main process within session
[[ ! -z "$PID" ]] && kill $SIGNAL $PID subpid=$(get_session_subpid $sname) # main process
tail --pid=$PID -f /dev/null abdpid=$(get_session_pid $sname) # abduco
[[ -a $session_path ]] && rm -f $session_path # kill it
[[ -a $fifo_path ]] && rm -f $fifo_path if [[ ${mode::1} == "k" ]]; then
[[ -f $log_path ]] && rm -f $log_path kill -SIGKILL $subpid
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
@ -334,6 +383,15 @@ case $mode in
abduco abduco
exit $EXIT_SUCCESS exit $EXIT_SUCCESS
;; ;;
"quit"|"q")
exit_if_not_in_session
session_name=$ABDUCO_SESSION
if is_logging $session_name; then
session log off
fi
echo "exit" | abduco -p $session_name
exit $EXIT_SUCCESS
;;
"help"|"h") "help"|"h")
print_help print_help
exit $EXIT_SUCCESS exit $EXIT_SUCCESS