How to stop a process when tmux session is detached - tmux

I often have several tmux sessions running at the same time, in most of these sessions I will have a local server running on a port. What I want is to be able to automatically kill this server when detaching from a tmux session and similarly re-start the server when re-attaching.
Is there anything in tmux that makes this easier, it feels like if there was a hook that tmux provided so that you could run a script before the session was detached or reattached that would do the trick but I can't see anything like this in the docs.

Fast forward to 2017.
Today tmux has hooks called client-attached and client-detached that run when clients attaches to the session or detaches from it, respectively.

tmux a # attach most recent tmux session
or
tmux list-sessions # look for the session you want to kill
tmux a -t 0 # attach session named "0"
Once inside the session you want to kill:
[Ctrl]-B x # kill current pane of current session
Do that for all the panes and windows within that session and that should do it.

You could add something like this to your ~/.tmux.conf
shell-command /bin/bash yourhook.sh
This would at least give you some functionality when running tmux, but I'm not sure what you would do about a detach hook...

Related

Making tmux do idle timeouts?

Is there any way to configure tmux to kill an idle session? I connect to a server with:
ssh -t host-name tmux new -A -s session-name
Most of the time, I want tmux to keep the session nailed up even if my ssh connection dies. This protects me against transient issues like my desktop crashing. That is, of course, the reason tmux exists.
But if I've simply forgotten to log out, I'd like the session to get killed after a sufficiently long idle time (say, 24 hours).

How to close all sessions properly using tmux?

I am using tmux version 2.7. What is a proper way to close all sessions so that there is no session at all running with tmux?
Any process running inside the session (such as a Rails server or Django server) should be terminated properly, without any of them dangling.
Ether of tmux kill-server or pkill tmux will do it (they have the same effect). All processes inside tmux will be sent SIGHUP by the kernel, what each does with it is up to the individual application.

tmux exit quickly and kill session

I have a few tmuxinator projects. When running one in tmux I would like to be able to exit it quickly (e.g. one key combination) and kill the session so that next time I open the project it starts in the same state. Detaching doesn't work as it doesn't reset the state.
Is this possible?
Just add bind k kill-session to .tmux.conf. And then you can exit and kill the session with PREFIX k.

How to watch a log file remotely for a period of time before disconnecting

I left out a very important part of this situation. The 'watch' command is part of a script that is scanning a series of hosts. The ssh sessions are created on the fly within the script. And there are several logs on each host that I'd like to watch. I've tried setting "ConnectTimeout=10" for instance to at least get it to move onto the next host but that doesn't affect how long it maintains the connection. Killing the ssh process seems to kill the script somehow.
Is there any way to use watch in an ssh session so that it only watches the target file for a set period of time before disconnecting? I know that you can use top in a batch manner and I'd like to be able to do the same thing with watch.
I don't see anything in the man pages and I haven't found anything in Internet searches where someone's been able to do this. Nor can I find an alternative to watch that can visually show changes to a file.
Any ideas?
You can simply kill the watch process after some time if that is all you need. If you run this as the command you pass to ssh so your ssh session will terminate when watch exits.
(sleep 3; kill `ps -C watch -o pid=`) & watch ls

Is there a way for a tmux session to find out if it is itself a nested tmux session?

If so, it will be possible to script keys which change which nested level of tmux handle the event based on context. e.g. I want a key to be handled by the host tmux if the nested inner tmux has only one pane open in its current window.
The host tmux can know that its current pane is running a tmux, which lets it "pass through" the key.
But now the child tmux, if its current window only has one pane, tell the host tmux "Oh thanks, I've only got one pane", so the host tmux can then carry on and do what it would normally do if its pane wasn't running tmux.
Now there's at least two ways: One is like I described, the host tmux always passes to the inner tmux. The problem here is I know not how the inner tmux can detect (1) that it is itself running in tmux, so it knows to report and (2) how to report to its host tmux any facts about its situation. For example my vimrc does this by calling out to tmux from within Vim (i.e. vimscript system('tmux select-pane .+')).
The other way is to somehow get the outer tmux to figure out which socket the inner tmux is, and call tmux -S innertmux-socket to deduce inner tmux's state, and based on that info determine whether to send in the command or not. There is no certainty to be had on the path of that socket based on its own tmux variables alone, but I think with some targeted shell scripting this won't be too hard.
Inception jokes welcome...
The question is serious though. This is no joke. Are there any other ways to achieve this?

Resources