You are not logged in.

#1 2016-01-07 23:17:38

rodrunner
Member
Registered: 2015-08-20
Posts: 14

[SOLVED] tmux autostart

Hi,

I was playing for a while with tmux and I would like to start it as a service when I log in.  I followed these hints in the ArchWiki and created the following unit in ~/.config/systemd/user/tmux.service:

[Unit]
Description=Start tmux in detached session

[Service]
Type=forking
User=%I
ExecStart=/usr/bin/tmux new-session -s %u -d
ExecStop=/usr/bin/tmux kill-session -t %u

[Install]
WantedBy=multi-user.target

I started/enabled the service and I see that it is actually running:

$ systemctl --user -l status tmux
● tmux.service - Start tmux in detached session
   Loaded: loaded (/home/rodrunner/.config/systemd/user/tmux.service; enabled; vendor preset: enabled)
   Active: active (running) since jue 2016-01-07 23:39:55 CET; 1min 10s ago
  Process: 631 ExecStart=/usr/bin/tmux new-session -s %u -d (code=exited, status=0/SUCCESS)
 Main PID: 633 (tmux)
   CGroup: /user.slice/user-1000.slice/user@1000.service/tmux.service
           ├─633 /usr/bin/tmux new-session -s rodrunner -d
           └─634 -bash

But the tmux session is detached, so that I need to attach to the running session first if I want to use tmux.

I've tried to attach in .bashrc but according to the message that I get I must be nesting one tmux session into another one:

sessions should be nested with care, unset $TMUX to force

Is there a clean way to attach to the tmux session directly?

Last edited by rodrunner (2016-01-08 15:13:50)

Offline

#2 2016-01-07 23:19:24

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] tmux autostart

What did you put in your .bashrc? And do you really want to attach to the same session every time you open a new shell?


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2016-01-07 23:41:10

rodrunner
Member
Registered: 2015-08-20
Posts: 14

Re: [SOLVED] tmux autostart

Thanks for the response.

No, I would not like to attach to the same tmux session in every new shell. I understand .bashrc is not the right place to do it, that's why I open the thread.
I was attaching to the tmux session (it has the name of my user) with

tmux attach -t $USER

Offline

#4 2016-01-08 00:25:43

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: [SOLVED] tmux autostart

I just use an alias:

mux() { [[ -z "$TMUX" ]] && { tmux attach -d || tmux -f $HOME/.tmux/conf new -s secured ;} }

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2016-01-08 01:16:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,529
Website

Re: [SOLVED] tmux autostart

I run tmux from my bashrc, but you need to test if it's already running - otherwise you'll just be running tmux within tmux within tmux ...

Tmux defines the environment variable TMUX, so just check for that:

[[ -z $TMUX ]] && exec tmux attach -t $USER

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2016-01-08 15:13:26

rodrunner
Member
Registered: 2015-08-20
Posts: 14

Re: [SOLVED] tmux autostart

Thanks a lot!

Offline

#7 2016-01-08 15:16:25

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,529
Website

Re: [SOLVED] tmux autostart

Note, JWRs point though: this will attach to the same session in every new terminal you open.  I prefer this - in fact I force a disconnect from all other terminals.  So when I open urxvt I get my tmux session.  If for any reason I open a new urxvt window (I don't, but if I did) it'd effectively just replace the other as the new one would connect to the existing tmux session, disconnect it from the first urxvt window, and because I run tmux with `exec` the first urxvt window then closes.

I have no need to have more than one terminal window: tmux manages my terminal "windows".


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2016-01-08 16:46:59

rodrunner
Member
Registered: 2015-08-20
Posts: 14

Re: [SOLVED] tmux autostart

also I would like to note that in order to add tmux  as a systemd user service I had to introduce a minor change in the sample service shown in the tmux page of the ArchWiki . The unit has to be loaded with default.target, since the multi-user.target does not apply in user systemd.

Offline

#9 2016-08-24 20:44:51

xand
Member
Registered: 2015-01-11
Posts: 16

Re: [SOLVED] tmux autostart

Guys, I just can't get the system unit to be started. I've tried to create the unit file with the exact same code of the rodrunner's first post and also with the suggested change on the last post. Also tried the wiki's suggestion to take out the `User=%I`. After reloading the daemon and starting the service on each of the 3 attempts, this is always the output of status:

→ systemctl --user status tmux.service
● tmux.service - Start tmux in detached session
   Loaded: loaded (/home/aleprovencio/.config/systemd/user/tmux.service; disabled; vendor prese
   Active: failed (Result: exit-code) since qua 2016-08-24 17:30:41 BRT; 47s ago

ago 24 17:30:40 xps systemd[797]: Starting Start tmux in detached session...
ago 24 17:30:40 xps systemd[797]: Started Start tmux in detached session.
ago 24 17:30:41 xps systemd-coredump[1900]: Process 1216 (tmux) of user 1000 dumped core.

                                            Stack trace of thread 1216:
                                            #0  0x00000000004379d4 n/a (tmux)
                                            #1  0x0000000000437af6 n/a (tmux)
                                            #2  0x000000000040a2a3 n/a (tmux)
                                            #3  0x0000000000410594 n/a (tmux)
                                            #4  0x0000000000431d8a n/a (tmux)
                                            #5  0x0000000000432407 n/a (tmux)
                                            #6  0x000000000042c3f0 n/a (tmux)
                                            #7  0x00007f89bf599fc1 event_base_loop (libevent-2.
                                            #8  0x000000000042c6cf n/a (tmux)
                                            #9  0x0000000000433e37 n/a (tmux)
                                            #10 0x0000000000406c98 n/a (tmux)
                                            #11 0x0000000000407067 n/a (tmux)
                                            #12 0x0000000000403f8b n/a (tmux)
                                            #13 0x00007f89beff4291 __libc_start_main (libc.so.6
                                            #14 0x0000000000404269 n/a (tmux)
ago 24 17:30:41 xps tmux[1971]: no server running on /tmp/tmux-1000/default
ago 24 17:30:41 xps systemd[797]: tmux.service: Control process exited, code=exited status=1
ago 24 17:30:41 xps systemd[797]: tmux.service: Unit entered failed state.
ago 24 17:30:41 xps systemd[797]: tmux.service: Failed with result 'exit-code'.

Offline

#10 2016-08-25 11:56:07

xand
Member
Registered: 2015-01-11
Posts: 16

Re: [SOLVED] tmux autostart

Nervermind, it was due to a plugin.

Offline

#11 2016-08-25 14:06:25

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,789

Re: [SOLVED] tmux autostart

Closed.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Online

Board footer

Powered by FluxBB