tmux-resurrect
tmux-resurrect copied to clipboard
How to restore from command line when starting tmux?
Ok, maybe I'm doing it wrong, but...
To restore a session from the bash prompt (after a fresh reboot), I run:
$ tmux new-session
Then I hit prefix ctrl-r
This restores all of my windows, but it also leaves me with the 'extra' window that was created with new-session
.
I kill the new window. This drops me out of tmux and back to the bash prompt. Then I have to run
$ tmux attach
Now I'm finally in my restored session without the extra new-session
window.
Is there any way to do all of this in one step? I'd love to have a bash script that does the following:
- attaches to a tmux session if it is running
- restores the saved session if no session is running
Hi, I happen to also do this everyday, have you found a better solution to automate this ?
Best
Nope, I haven't found a better solution.
But I now run:
$ tmux new && tmux attach
Which at least saves one step. :)
After thinking a bit, I found a solution :
alias ma='tmux attach || { (while ! tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh; do sleep 0.2; done)& tmux ; }'
It will basically do :
tmux attach
, eg attach to an existing session.
If no session exists, it will run tmux, and also try to run the restore.sh script until it succeeds, in a subshell running in the background,
This gives me a looping error every that there is no server running on /tmp/tmux-nnnnnn/default
, interspersed with the occasional lost server
. I tried:
tmux -Lx start \; run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh \; attach
but still get lost server
.
This gives me a looping error every that there is no server running on /tmp/tmux-nnnnnn/default, interspersed with the occasional lost server. I tried:
Oh that is strange, does the tmux session open ?
My script might be bash specific, I'm running on bash, maybe you're running on an other shell ?
No, the session doesn't open.
I'm using zsh, but I tried in bash and get the same result. I'm also running via cygwin on Windows, which is usually the cause of any unexpected error.
If the session doesn't even open with my script, it means that the command is not run in the background :
(while ! tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh; do sleep 0.2; done)&
maybe this has to do with bash on windows. You could try to put the following code in an external script
runresurrect
while ! tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh; do sleep 0.2; done
and then try running
runresurrect & ; tmux
@edi9999 your alias solution works for me on bash/ubuntu. This was annoying me forever!
I have this snippet in my .bash_profile which does the trick:
if [[ -z "$TMUX" ]] ;then
ID="$( tmux ls | grep -vm1 attached | cut -d: -f1 )" # get the id of a deattached session
if [[ -z "$ID" ]] ;then # if not available create a new one
tmux new-session
else
tmux attach-session -t "$ID" # if available attach to it
fi
fi
Credits to https://wiki.archlinux.org/index.php/tmux#Start_tmux_on_every_shell_login
Try this. This works well for me -- so far haven't had any issues.
alias mux='pgrep -vxq tmux && tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh && tmux kill-session -t delete-me && tmux attach || tmux attach'
@mschenk42
Thanks, your solution is the only one I found usable.
Though I don't see any documentation for the -q
flag. pgrep
also yells for providing an invalid flag.
I'm assuming it was intended to be a "quiet" flag.
What I've done instead is output to /dev/null
:
pgrep -vx tmux > /dev/null
Well after all this time searching the easiest way for me personally is to just add this line at the end in your .tmux.conf
file
run -b '~/.tmux/plugins/tmux-resurrect/scripts/restore.sh r'
@sujayshaunak thanks for the solutions works like a charm
Well after all this time searching the easiest way for me personally is to just add this line at the end in your
.tmux.conf
file
run -b '~/.tmux/plugins/tmux-resurrect/scripts/restore.sh r'
This adds a new session before resurrect. Which is the main subject of the issue. -or do I miss a point?
@mschenk42
Thanks, your solution is the only one I found usable.
Though I don't see any documentation for the
-q
flag.pgrep
also yells for providing an invalid flag.I'm assuming it was intended to be a "quiet" flag.
What I've done instead is output to
/dev/null
:
pgrep -vx tmux > /dev/null
This worked for me as well. Thanks for the solution. It was an annoying everyday issue.
alias mux='pgrep -vx tmux > /dev/null/ && tmux new -d -s delete-me && tmux run-shell ~/.tmux/plugins/tmux-resurrect/scripts/restore.sh && tmux kill-session -t delete-me && tmux attach || tmux attach'
Thanks for the solution @mschenk42.
I've added the alias:
alias mux='pgrep -vx tmux > /dev/null && \
tmux new -d -s delete-me && \
tmux run-shell $TMUX_DIR/plugins/tmux-resurrect/scripts/restore.sh && \
tmux kill-session -t delete-me && \
tmux attach || tmux attach'
The redirection to /dev/null
is needed to dump output from pgrep
.
I think @sujayshaunak 's solution is standard, uses the appropriate place for initialization instead of hacks. Not to mention it works across shells (I was about to pull my hair out translating the bash alias to a fish one).
I think it should be added to the documentation and this issue can be closed.
This solution still creates an empty session though, and I guess there's no way to get rid of that without complicating.
There was a recent fix that removes empty session 0
after restore.
There was a recent fix that removes empty session
0
after restore.
This doesn't work if you're sorting your sessions by name lexicographically and the dummy session is no longer session 0
, like so:
bind s choose-tree -sZ -O name