tmux-resurrect icon indicating copy to clipboard operation
tmux-resurrect copied to clipboard

How to restore from command line when starting tmux?

Open mgraham opened this issue 8 years ago • 18 comments

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

mgraham avatar Apr 06 '16 15:04 mgraham

Hi, I happen to also do this everyday, have you found a better solution to automate this ?

Best

edi9999 avatar Jun 27 '16 21:06 edi9999

Nope, I haven't found a better solution.

But I now run:

$ tmux new && tmux attach

Which at least saves one step. :)

mgraham avatar Jun 30 '16 03:06 mgraham

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,

edi9999 avatar Aug 23 '16 12:08 edi9999

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.

nickspoons avatar Aug 23 '16 22:08 nickspoons

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 ?

edi9999 avatar Aug 24 '16 06:08 edi9999

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.

nickspoons avatar Aug 24 '16 23:08 nickspoons

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 avatar Aug 25 '16 11:08 edi9999

@edi9999 your alias solution works for me on bash/ubuntu. This was annoying me forever!

ptsteadman avatar Jun 07 '17 02:06 ptsteadman

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

danielcondemarin avatar Jun 16 '18 10:06 danielcondemarin

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 avatar Feb 16 '19 22:02 mschenk42

@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

IgorGee avatar Aug 09 '19 22:08 IgorGee

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 avatar Oct 17 '19 06:10 sujayshaunak

@sujayshaunak thanks for the solutions works like a charm

saravanaskanda avatar Nov 27 '19 04:11 saravanaskanda

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'

SerhatTeker avatar Dec 27 '19 15:12 SerhatTeker

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.

abhirup-dev avatar Dec 29 '19 04:12 abhirup-dev

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.

peey avatar Sep 25 '20 16:09 peey

There was a recent fix that removes empty session 0 after restore.

bruno- avatar Sep 26 '20 09:09 bruno-

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

tblancher avatar May 03 '24 09:05 tblancher