crigler / dtach

A simple program that emulates the detach feature of screen
GNU General Public License v2.0
476 stars 50 forks source link

can't find a way to run command inside session from script #19

Open jan-sopi opened 1 year ago

jan-sopi commented 1 year ago

Hi, I couldn't find any way to attach to a session and execute a command from a script. I tried searching for help or trying out stuff on my own, but nothing work, is there no way to do this? I found that screen has the -dm switches that serve for this purpose, so it'd be nice to have something similar.

xPMo commented 1 year ago

Does dtach -n do what you want? Like maybe dtach -n /tmp/example.sock htop? There's also -N if you want dtach to wait until the program exits.

  • -n Creates a new session, without attaching to it. A new session is created in which the specified program is executed. dtach does not try to attach to the newly created session, however, and exits instead.
  • -N Creates a new session, without attaching to it or daemonizing. A new session is created in which the specified program is executed. dtach does not try to attach to the newly created session, however, and will stay in the foreground until the program exits.
jan-sopi commented 1 year ago

@xPMo

Does dtach -n do what you want? Like maybe dtach -n /tmp/example.sock htop? There's also -N if you want dtach to wait until the program exits.

I did see the -n switch, but the problem is that it tries to create a new socket, while I wanted it to attach to an already existing one. I could maybe delete the socket and make it again, but that doesn't seem like a very elegant and practical solution.

xPMo commented 1 year ago

it tries to create a new socket

That is analogous to screen -dm, which creates a new session.

I wanted it to attach to an already existing one.

Note that dtach is not a multiplexer: each socket controls exactly one session. Each session starts exactly one command, and exits when that command exits. When you talk about an "existing one", that socket is for talking to a session which already has a program running in it.


Could you provide more details? What core problem are you trying to solve with dtach?

jan-sopi commented 1 year ago

Note that dtach is not a multiplexer: each socket controls exactly one session. Each session starts exactly one command, and exits when that command exits. When you talk about an "existing one", that socket is for talking to a session which already has a program running in it.

I realize, but all I want to do is connect to a session and execute a script without having any other commands running or computers connected to said session

Could you provide more details? What core problem are you trying to solve with dtach?

I'm trying to restart a minecraft server. to do this my plan was to periodically run a script that stops it, connects to the session that was running the server and makes backups, updates stuff and starts the server again before disconnecting from the session. my solution rn is to, after stopping the server, deleting the socket and making it again using dtach -n like someone mentioned. however it's quite unnecessary and could cause some corruption in case the server doesn't stop like it should

rickyson96 commented 4 months ago

hey @jan-sopi

I'm not particularly familiar with dtach here as I'm just trying to use it.

But can you do it like the example from the README?

dtach also has a mode that copies the contents of standard input to a session.
For example:

    $ echo -ne 'cd /var/log\nls -l\n' | dtach -p /tmp/foozle

The contents are sent verbatim including any embedded control characters (e.g.
the newline characters in the above example), and dtach will not scan the
input for a detach character.
rdebath commented 3 months ago

Actually, it sounds like OP was trying to connect to a "session" after the "minecraft server" had exited. This would normally mean that the socket had been deleted by dtach so there's some confusion as to what exactly is running at that point.

Reading it again I think they have started dtach with a bash command and run the MCS from that command line; they now have the problem that bash is doing nothing but waiting for the next command. So their actual solution would be to run the MCS directly from dtach eg: dtach -n /tmp/mcsock java minecraft.jar so that dtach and the socket disappear when MCS stops. Your command would still be a workaround, but has the issue that they are not sure if it's bash or MCS that is receiving the standard input.

NB: I don't think this server will redraw the screen on reconnection so I personally would use screen so I get some scrollback.