From 3dae8fd189b392c9ac16c2c761f0c0d2cc3847d6 Mon Sep 17 00:00:00 2001 From: Zach Dykstra Date: Thu, 19 Mar 2020 19:31:21 -0500 Subject: [PATCH 1/3] initial logic to clean up ephemeral sessions on exit --- tmuxc | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tmuxc b/tmuxc index a9ede15..0481ede 100755 --- a/tmuxc +++ b/tmuxc @@ -83,7 +83,16 @@ GetOptions( "exitlast|E" => \$cli{exitlast}, "print|o" => \$cli{printOptions}, "swm|w" => \$cli{swm}, - "ephemeral|S" => sub { + "poe|P=s" => sub { + ( undef, my $scope ) = @_; + unless ( $scope =~ m/(none|ephemeral|any)/ ) { + print "Invalid purge on exit scope: $scope\n"; + exit; + } else { + $cli{poe} = $scope; + } + }, + "ephemeral|S" => sub { $cliAction = \&EphemeralSession; }, "session|s=s" => sub { @@ -537,6 +546,17 @@ MAINEND: END { exit if $noclean; + + if ( ( $config->{poe} eq "any" ) + or ( $config->{ephemeral} and $config->{poe} eq "ephemeral" ) ) + { + my @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{control}, ] ); + Log( LOG_DEBUG, \@cmd ); + + @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{session}, ] ); + Log( LOG_DEBUG, \@cmd ); + } + kill 'INT', @children if scalar(@children); CleanExit; } @@ -577,6 +597,7 @@ sub LoadConfig { "prettyps" => 1, "swm" => 0, "on_connect" => [qw()], + "poe" => "none", ); # Read in the config file @@ -620,6 +641,9 @@ sub LoadConfig { my $sess = $config->{session}; + # Record if this is an ephemeral session + $config->{ephemeral} = ( defined( $config->{hosts}{$chost}{$sess} ) ) ? 0 : 1; + # Look up a number of keys that can be set at different levels # If no value can be found, use a sane default foreach my $key ( keys %defaultOptions ) { From 210e6151a1e151656c23a7cbc9c8b91ef1e45b1c Mon Sep 17 00:00:00 2001 From: Zach Dykstra Date: Fri, 20 Mar 2020 09:52:27 -0500 Subject: [PATCH 2/3] Order of operations matters here, apparently --- tmuxc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tmuxc b/tmuxc index 0481ede..a7a2984 100755 --- a/tmuxc +++ b/tmuxc @@ -550,11 +550,13 @@ END { if ( ( $config->{poe} eq "any" ) or ( $config->{ephemeral} and $config->{poe} eq "ephemeral" ) ) { - my @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{control}, ] ); + my @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{session}, ] ); Log( LOG_DEBUG, \@cmd ); + qx(@cmd); - @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{session}, ] ); + @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{control}, ] ); Log( LOG_DEBUG, \@cmd ); + qx(@cmd); } kill 'INT', @children if scalar(@children); From 85eeb0d39b40ed659751f082274383d8492823b8 Mon Sep 17 00:00:00 2001 From: Zach Dykstra Date: Sat, 28 Mar 2020 12:51:23 -0500 Subject: [PATCH 3/3] Make the debug log a bit more readable --- README.md | 1 + tmuxc | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ccd83a..9f6a2be 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ If the default option is acceptable, you do not need to define it again in a con * `on_connect (default[])`: When operating in single-window-mode, define an array of tmux commands to be sent to the cloned session to which you are attached. This option is only executed when `swm` is true. * `persist (default 10s)`: Set the SSH `ControlPersist` option for control master connections. * `prettyps (default 1)`: Control the process name on the command line, reducing it to `binaryname session@host`. +* `poe (default none)`: Enable purge-on-exit for `ephemeral` or `all` sessions. This will execute a `kill-session` against the session on exit. * `reconnect (default 1)`: Attempt to reconnect to the remote host and re-open windows if SSH drops. This is useful for laptops that are suspended frequently. * `selector (default: rofi -dmenu -i)`: Define the menu command, used for picking a session / session action. * `skipnw (default 0)`: Do not launch a new terminal when a new window is created in tmux. diff --git a/tmuxc b/tmuxc index a7a2984..5436f78 100755 --- a/tmuxc +++ b/tmuxc @@ -551,11 +551,11 @@ END { or ( $config->{ephemeral} and $config->{poe} eq "ephemeral" ) ) { my @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{session}, ] ); - Log( LOG_DEBUG, \@cmd ); + Log( LOG_DEBUG, "Killing session $config->{session}" ); qx(@cmd); @cmd = buildCommand( $host, [ $config->{tmux_bin}, qw(kill-session -t), $config->{control}, ] ); - Log( LOG_DEBUG, \@cmd ); + Log( LOG_DEBUG, "Killing control session $config->{control}" ); qx(@cmd); } @@ -1538,6 +1538,10 @@ Open up a menu listing sessions from the .tmuxc.conf file that aren't running. Open up a menu listing control options. If one instance is running, the command is executed on that instance. If multiple are running, prompt the running sessions. +=item B<--poe|P> + +Enable purge-on-exit for sessions of type 'none', 'ephemeral' or 'all'. This executes a I against the session on tmuxc exit. + =item B<--singlemenu|M> Open up a menu listing control options. The options take the form of session@host % , and are shown as a single-depth menu. Smart pagers (rofi, bemenu, etc) allow you to type partial words in any order to narrow down the option to execute.