Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi-user environments #49

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion continuum.tmux
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -x
set +x

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Expand Down
42 changes: 40 additions & 2 deletions scripts/continuum_save.sh
Original file line number Diff line number Diff line change
@@ -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"
}
Expand All @@ -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)"
Expand All @@ -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 [ "$DEBUG" == "2" ]; 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
}
Expand Down
13 changes: 8 additions & 5 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ 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 -u $UID -o "command pid uid" |\
grep -E '^tmux' |\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add -E flag?

grep -vE '^tmux\s+(a|source)'
}

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"
}
Expand Down
4 changes: 4 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down