From 351b80109fcc83476b239bf94718bc280e2372bd Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Sun, 29 Apr 2018 02:10:54 +0000 Subject: [PATCH 1/7] Refactor `all_tmux_processes` When counting the number of running tmux processes, count only those that belong to the current user. `tmux a` shows as a separate process (must be the client process) and thus needs to be excluded from the calculation as well. The previous exclusion of `tmux source` was also incorporated. --- scripts/helpers.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index b64fb36..607268f 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -23,10 +23,14 @@ current_tmux_server_pid() { } all_tmux_processes() { - # ignores `tmux source-file .tmux.conf` command used to reload tmux.conf - ps -Ao "command pid" | - \grep "^tmux" | - \grep -v "^tmux source" + # finds all tmux processes for the current user ignoring the following: + # 1) `tmux source-file .tmux.conf` (used to reload tmux.conf) + # 2) `tmux a` (which shows an additional process) + + ps -Ao "command pid uid" |\ + grep $UID |\ + grep -E '^tmux' |\ + grep -vE '^tmux\s+(a|source)' } number_tmux_processes_except_current_server() { From fc73e092e5b47c437d2dad72893091e41e361c23 Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Fri, 22 Jun 2018 17:15:39 +0000 Subject: [PATCH 2/7] Use the `-u` switch of `ps` to limit `ps` scope to that of the current user. --- scripts/helpers.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 607268f..d87a42d 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -27,8 +27,7 @@ all_tmux_processes() { # 1) `tmux source-file .tmux.conf` (used to reload tmux.conf) # 2) `tmux a` (which shows an additional process) - ps -Ao "command pid uid" |\ - grep $UID |\ + ps -u $UID -o "command pid uid" |\ grep -E '^tmux' |\ grep -vE '^tmux\s+(a|source)' } From 82d8ad5ef092362d824b6417656b95b39a28eb62 Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Sat, 6 Oct 2018 19:11:46 +0000 Subject: [PATCH 3/7] Add infrastructure for debugging. --- scripts/continuum_save.sh | 42 +++++++++++++++++++++++++++++++++++++-- scripts/variables.sh | 4 ++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/scripts/continuum_save.sh b/scripts/continuum_save.sh index c85f68a..ed681e5 100755 --- a/scripts/continuum_save.sh +++ b/scripts/continuum_save.sh @@ -1,11 +1,25 @@ #!/usr/bin/env bash - +set +x CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" source "$CURRENT_DIR/variables.sh" source "$CURRENT_DIR/shared.sh" +DEBUG=$(get_tmux_option "$debug") + +get_log_path() { + get_tmux_option "$log_path" "$log_path_default" +} + +log_message() { + log_path=$(get_log_path) + if [ "$DEBUG" == "1" ] && [ -n "$log_path" ]; then + message="$@" + echo "$message" >> $log_path + fi +} + supported_tmux_version_ok() { "$CURRENT_DIR/check_tmux_version.sh" "$SUPPORTED_VERSION" } @@ -18,6 +32,14 @@ auto_save_not_disabled() { [ "$(get_interval)" -gt 0 ] } +get_next_run() { + local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")" + local interval_minutes="$(get_interval)" + local interval_seconds="$((interval_minutes * 60))" + local next_run="$((last_saved_timestamp + $interval_seconds))" + echo $next_run +} + enough_time_since_last_run_passed() { local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")" local interval_minutes="$(get_interval)" @@ -29,13 +51,29 @@ enough_time_since_last_run_passed() { fetch_and_run_tmux_resurrect_save_script() { local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")" if [ -n "$resurrect_save_script_path" ]; then - "$resurrect_save_script_path" "quiet" >/dev/null 2>&1 & + if [ -n "$DEBUG" ]; then + local log_path=$(get_log_path) + log_message "Calling $resurrect_save_script_path" + "$resurrect_save_script_path" >> $log_path 2>&1 & + else + "$resurrect_save_script_path" "quiet" >/dev/null 2>&1 & + fi set_last_save_timestamp fi } main() { + if [ -n "$DEBUG" ]; then + TS_NEXT=$(get_next_run) + TIME_NEXT=$(date -d \@"$TS_NEXT" +"%Y-%m-%d at %H:%M:%S") + MSG="Next save on $TIME_NEXT" + log_message "$(date +'%Y-%m-%d %H:%M:%S'): $MSG" + fi if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed; then + if [ -n "$DEBUG" ]; then + log_message "Actual run on $(date)" + echo "Saved on $(date +'%Y-%m-%d@%H:%M:%S')" + fi fetch_and_run_tmux_resurrect_save_script fi } diff --git a/scripts/variables.sh b/scripts/variables.sh index 8d0a057..9eb0d46 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -1,5 +1,9 @@ SUPPORTED_VERSION="1.9" +log_path="@continuum-log-path" +log_path_default="${HOME}/tmux-continuum.log" +debug="@continuum-debug" + # these tmux options contain paths to tmux resurrect save and restore scripts resurrect_save_path_option="@resurrect-save-script-path" resurrect_restore_path_option="@resurrect-restore-script-path" From eb07eef2b845151cbd8ddb6a50b76a0d1c08c791 Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Wed, 10 Oct 2018 02:31:19 +0000 Subject: [PATCH 4/7] Turn off execution tracing (-x) --- continuum.tmux | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/continuum.tmux b/continuum.tmux index e8d60e3..91ef2ba 100755 --- a/continuum.tmux +++ b/continuum.tmux @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -x +set +x CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" From ef62ce199a04ba6f50b735e20a76c459622dcd73 Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Tue, 9 Jun 2020 15:46:06 +0000 Subject: [PATCH 5/7] Show "Saving" message only when DEBUG=1 --- scripts/continuum_save.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/continuum_save.sh b/scripts/continuum_save.sh index ed681e5..778b1bd 100755 --- a/scripts/continuum_save.sh +++ b/scripts/continuum_save.sh @@ -51,7 +51,7 @@ enough_time_since_last_run_passed() { fetch_and_run_tmux_resurrect_save_script() { local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")" if [ -n "$resurrect_save_script_path" ]; then - if [ -n "$DEBUG" ]; then + if [ "$DEBUG" == "1" ]; then local log_path=$(get_log_path) log_message "Calling $resurrect_save_script_path" "$resurrect_save_script_path" >> $log_path 2>&1 & From a9785b4e2d0f2252f0de96ccba6f306fab2d391c Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Sat, 24 Apr 2021 23:25:11 -0500 Subject: [PATCH 6/7] Remove bug in grep pattern The pid the `grep -v` is trying to eliminate is not at the end of the line. `all_tmux_processes` produces the following output (for example): ``` tmux 32069 29270 ``` Here the first number is the pid and the second -- the uid. So, since the idea of `number_tmux_processes_except_current_server` is to eliminate 32069 from consideration, it should look for "PID". --- scripts/helpers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index d87a42d..ad808d2 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -34,7 +34,7 @@ all_tmux_processes() { number_tmux_processes_except_current_server() { all_tmux_processes | - \grep -v " $(current_tmux_server_pid)$" | + \grep -v " $(current_tmux_server_pid) " | wc -l | sed "s/ //g" } From feb4333eba74a0c106154bfc929a3d94fd1b3583 Mon Sep 17 00:00:00 2001 From: Sergei Gerasenko Date: Sat, 24 Apr 2021 23:39:31 -0500 Subject: [PATCH 7/7] Show "Saving" message only when DEBUG=2 This is logical because we're increasing verbosity by setting the value to 2. At 1, the debug is on but the user is not interrupted by the status line messages. --- scripts/continuum_save.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/continuum_save.sh b/scripts/continuum_save.sh index 778b1bd..2b27498 100755 --- a/scripts/continuum_save.sh +++ b/scripts/continuum_save.sh @@ -51,7 +51,7 @@ enough_time_since_last_run_passed() { fetch_and_run_tmux_resurrect_save_script() { local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")" if [ -n "$resurrect_save_script_path" ]; then - if [ "$DEBUG" == "1" ]; then + if [ "$DEBUG" == "2" ]; then local log_path=$(get_log_path) log_message "Calling $resurrect_save_script_path" "$resurrect_save_script_path" >> $log_path 2>&1 &