Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to restore from command line when starting tmux? #139

Open
mgraham opened this issue Apr 6, 2016 · 15 comments
Open

How to restore from command line when starting tmux? #139

mgraham opened this issue Apr 6, 2016 · 15 comments

Comments

@mgraham
Copy link

@mgraham mgraham commented Apr 6, 2016

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
@edi9999

This comment has been minimized.

Copy link

@edi9999 edi9999 commented Jun 27, 2016

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

Best

@mgraham

This comment has been minimized.

Copy link
Author

@mgraham mgraham commented Jun 30, 2016

Nope, I haven't found a better solution.

But I now run:

$ tmux new && tmux attach

Which at least saves one step. :)

@edi9999

This comment has been minimized.

Copy link

@edi9999 edi9999 commented Aug 23, 2016

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,

@nickspoons

This comment has been minimized.

Copy link

@nickspoons nickspoons commented Aug 23, 2016

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.

@edi9999

This comment has been minimized.

Copy link

@edi9999 edi9999 commented Aug 24, 2016

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 ?

@nickspoons

This comment has been minimized.

Copy link

@nickspoons nickspoons commented Aug 24, 2016

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.

@edi9999

This comment has been minimized.

Copy link

@edi9999 edi9999 commented Aug 25, 2016

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

@ptsteadman

This comment has been minimized.

Copy link

@ptsteadman ptsteadman commented Jun 7, 2017

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

@danielcondemarin

This comment has been minimized.

Copy link

@danielcondemarin danielcondemarin commented Jun 16, 2018

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

@mschenk42

This comment has been minimized.

Copy link

@mschenk42 mschenk42 commented Feb 16, 2019

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'
@IgorGee

This comment has been minimized.

Copy link

@IgorGee IgorGee commented Aug 9, 2019

@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

@sujayshaunak

This comment has been minimized.

Copy link

@sujayshaunak sujayshaunak commented Oct 17, 2019

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'

@saravanavi

This comment has been minimized.

Copy link

@saravanavi saravanavi commented Nov 27, 2019

@sujayshaunak thanks for the solutions works like a charm

@SerhatTeker

This comment has been minimized.

Copy link

@SerhatTeker SerhatTeker commented Dec 27, 2019

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'
@maverickdas

This comment has been minimized.

Copy link

@maverickdas maverickdas commented Dec 29, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.