From 05a4a827e5c1395cc413102ff9cb830dc333e96b Mon Sep 17 00:00:00 2001 From: Pete Peteches McCabe Date: Mon, 23 Nov 2015 18:28:38 +0000 Subject: [PATCH] initial stab at systemd support for tmux-continuum --- continuum.tmux | 2 + scripts/handle_tmux_automatic_start.sh | 10 ++++ .../systemd_disable.sh | 10 ++++ .../systemd_enable.sh | 57 +++++++++++++++++++ scripts/variables.sh | 3 + 5 files changed, 82 insertions(+) create mode 100755 scripts/handle_tmux_automatic_start/systemd_disable.sh create mode 100755 scripts/handle_tmux_automatic_start/systemd_enable.sh diff --git a/continuum.tmux b/continuum.tmux index 3f97736..e8d60e3 100755 --- a/continuum.tmux +++ b/continuum.tmux @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -x + CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/scripts/helpers.sh" diff --git a/scripts/handle_tmux_automatic_start.sh b/scripts/handle_tmux_automatic_start.sh index 35acae9..8f3c72d 100755 --- a/scripts/handle_tmux_automatic_start.sh +++ b/scripts/handle_tmux_automatic_start.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -x + CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "$CURRENT_DIR/helpers.sh" @@ -14,14 +16,22 @@ is_osx() { [ $(uname) == "Darwin" ] } +is_systemd() { + [ $(ps -o comm= -p1) == 'systemd' ] +} + main() { if is_tmux_automatic_start_enabled; then if is_osx; then "$CURRENT_DIR/handle_tmux_automatic_start/osx_enable.sh" + elif is_systemd; then + "$CURRENT_DIR/handle_tmux_automatic_start/systemd_enable.sh" fi else if is_osx; then "$CURRENT_DIR/handle_tmux_automatic_start/osx_disable.sh" + elif is_systemd; then + "$CURRENT_DIR/handle_tmux_automatic_start/systemd_disable.sh" fi fi } diff --git a/scripts/handle_tmux_automatic_start/systemd_disable.sh b/scripts/handle_tmux_automatic_start/systemd_disable.sh new file mode 100755 index 0000000..115e1c9 --- /dev/null +++ b/scripts/handle_tmux_automatic_start/systemd_disable.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/../variables.sh" + +main() { + systemctl --user disable ${systemd_service_name} +} +main diff --git a/scripts/handle_tmux_automatic_start/systemd_enable.sh b/scripts/handle_tmux_automatic_start/systemd_enable.sh new file mode 100755 index 0000000..4fcd816 --- /dev/null +++ b/scripts/handle_tmux_automatic_start/systemd_enable.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +set -x + +CURRENT_DIR="$( dirname ${BASH_SOURCE[0]} )" + +source "$CURRENT_DIR/../helpers.sh" +source "$CURRENT_DIR/../variables.sh" + +template() { + local tmux_start_script="$1" + shift + local options="$@" + local content="" + + read -r -d '' content <<-EOF + [Unit] + Description=tmux default session (detached) + Documentation=man:tmux(1) + + [Service] + Type=oneshot + RemainAfterExit=True + Environment=DISPLAY=:0 + ExecStart=/usr/bin/tmux ${systemd_tmux_server_start_cmd} + ExecStartPost=/usr/bin/tmux run /home/peteches/.tmux/plugins/tmux-resurrect/scripts/restore.sh + + ExecStop=/home/peteches/.tmux/plugins/tmux-resurrect/scripts/save.sh + ExecStop=/usr/bin/tmux kill-server + KillMode=none + + [Install] + WantedBy=default.target + EOF + + echo "$content" +} + +systemd_tmux_is_enabled() { + systemctl --user is_enabled $(basename "${systemd_unit_file_path}") >/dev/null 2>&1 +} + +enable_tmux_unit_on_boot() { + if ! systemd_tmux_is_enabled; then + systemctl --user enable ${systemd_service_name} + fi +} + +main() { + local options="$(get_tmux_option "$auto_start_config_option" "${auto_start_config_default}")" + local systemd_tmux_server_start_cmd="$(get_tmux_option "${systemd_tmux_server_start_cmd}" "${systemd_tmux_default_server_start_cmd}" )" + local tmux_start_script_path="${CURRENT_DIR}/linux_start_tmux.sh" + local systemd_unit_file=$(template "${tmux_start_script_path}" "${options}") + echo "$systemd_unit_file" > "${systemd_unit_file_path}" + enable_tmux_unit_on_boot +} +main diff --git a/scripts/variables.sh b/scripts/variables.sh index 52597fd..be743c9 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -32,3 +32,6 @@ status_script="#($CURRENT_DIR/scripts/continuum_status.sh)" status_on_style_wrap_option="@continuum-status-on-wrap-style" # example value: "#[fg=green]#{value}#[fg=white]" status_off_style_wrap_option="@continuum-status-off-wrap-style" # example value: "#[fg=yellow,bold]#{value}#[fg=white,nobold]" status_wrap_string="\#{value}" + +systemd_service_name="tmux.service" +systemd_unit_file_path="$HOME/.config/systemd/user/${systemd_service_name}"