6

I'd like to start a screen session inside an LXD-managed container so that I could detach running processes and maybe re-attach in the future when I want to check on their status.

I tried to attach to the container running

$ lxc exec my-ubuntu -- /bin/bash

and then start a session with screen -x, but I get the following error:

Must be connected to a terminal.

I can see that many people have had a similar problem when trying to initiate screen from ssh, but I couldn't apply the suggested solution to my case.

Jonathan Y.
  • 1,034
  • 1
  • 16
  • 35

2 Answers2

7

For LXD using

lxc exec my-ubuntu -- sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -s /bin/bash"

or

lxc exec my-ubuntu -- sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -x"

or with any other screen switch.


The same for Docker ;)

docker run -it my-ubuntu sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -s /bin/bash"
A.B.
  • 89,123
  • 21
  • 245
  • 323
  • Terrific. But how can I then work with `screen` the way I normally do, monitor which sessions are running and re-attach to a specific one? – Jonathan Y. Jan 02 '16 at 19:40
  • I don't understand. In the same way as before. E.g. using `-R`. – A.B. Jan 02 '16 at 19:42
  • Answer updated. – A.B. Jan 02 '16 at 19:45
  • Well, I would normally do `screen -x`, but trying `lxc exec my-ubuntu -- sh -c "exec >/dev/tty 2>/dev/tty /dev/tty 2>/dev/tty .tty.my-ubuntu"`. Nor can I do `screen -x` from within bash in the container. – Jonathan Y. Jan 02 '16 at 19:48
  • What's about `-R` – A.B. Jan 02 '16 at 19:51
  • I should specify: the reason `... screen -x` fails is because I get `There are several suitable screens on:` followed by a list of sessions. Same with `screen -R`. – Jonathan Y. Jan 02 '16 at 19:51
  • Perhaps you should ask a new question for this problem. :) seems to be a little bit complicated. – A.B. Jan 02 '16 at 19:52
  • There aren't any answers there, but I'd like to mention that I did open a new question regarding this issue [here](http://askubuntu.com/q/716106/142526). I also suggested a walk-around on that question. – Jonathan Y. May 09 '16 at 11:26
  • @JonathanY. just run script /dev/null and then run screen. – LUser Aug 18 '21 at 18:47
-1

For me, the simple solution was to start screen on the host instead of in the container.

user@localhost:~$ screen
user@localhost:~$ lxc-attach -n yourcontainer
root@container:~# ./start-gameserver.sh
root@container:~# #Use "Ctrl+A:sessionname mygame<Enter>" to set a screen name
root@container:~# #and use the usual "Ctrl+A,D" to disconnect from screen
user@localhost:~$ screen -ls
There is a screen on:
    25418.mygame    (04/15/2019 11:41:56 PM)    (Detached)
user@localhost:~$ screen -r mygame
root@container:~# #etc.
Luc
  • 903
  • 10
  • 18