Is it possible to execute commands on tmux detach?

I usually have a rails console and server running in the tmux session, on detach is it possible to automatically end those?

  • 5
    The whole point of detaching from a session is that programs running inside the session are unaware that it has happened.
    – chepner
    Apr 9, 2014 at 13:49

1 Answer 1


AFAICT, tmux didn't provide hooking on detach, attach events. But you can always send a command to a detached session, using tmux send:

tmux send -t session_name.panel '<command><Enter>'


$ tmux new -s foo

Pressing bind-key + d to detach. Then:

$ tmux send -t foo 'echo foo<Enter>'
$ tmux attach -t foo

and you will see the word foo.

Note that <Enter> here mean you type the literal Enter.

  • 1
    can we automate these tasks without touching any keys?
    – 32r34wgf3e
    Oct 30, 2017 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.