1

I know screen -dmS mysession command to start a screen, launch a command and detach. When we need to redirect the stdout to a file, this works: (see How to run a program in a screen, redirect all output to a file and detach)

screen -dmS mysession bash -c 'ls > test.txt'      # simple example

However at the end, the screen session doesn't stay, we cannot do screen -r mysession, it doesn't exist anymore in screen -ls.

Question: in a one-liner, how to start a screen session, launch a command, detach, redirect stdout to a file (like > test.txt) but don't automatically exit the screen session when finished?

Linked question: How to prevent "screen -dmS sessionName program.sh" from disappearing after program.sh finishes?

Basj
  • 2,519
  • This probably depends on what you want to achieve with this setting. The screen session ends because the bash you invoked terminates after the command (ls > text.txt) exits. But so does the input to your test.txt, so the question is - do you want the output of all further commands you would then interactively want to enter at the bash prompt to also appear in test.txt, or are you satisfied with just the output of the ls command ending up there, and simply want to have an open screen session available? – AdminBee May 20 '22 at 10:38
  • @AdminBee I want to be able to see for example the error log (stderr) that would not be saved to test.txt. For example this works thanks to your link screen -dmS mysession bash -c 'ls -laW > test.txt; exec bash': ls -laW does an error, and we can see it when doing screen -r mysession. Great! Only remaining problem: https://unix.stackexchange.com/questions/703246/why-are-keystrokes-not-intercepted-after-reattaching-a-screen-dms-session-bash – Basj May 20 '22 at 13:44

1 Answers1

4

Using "bash -c" results in a non-interactive shell. You're creating a process tree as such:

screen───bash───ls

When ls finishes, bash has no further process and no further input and finishes also.

Thus screen finishes.

To do what you want to do, you'd need to start a interactive shell first.

screen -dmS session bash

Then input your commands into the screen session using screens stuff command.

screen -S session -p 0 -X stuff "ls > test.txt^M"

The ^M acts like a newline.

You'd need to quit the shell using exit^M or the likes afterwards.

You can just combine all of into a one-liner afterwards IE

screen -dmS session bash && screen -S session -p 0 -X stuff "ls > test.txt^M"

Honestly I think for what you're trying todo however its probably better to avoid screen entirely and just disown whatever process it is you want to work on.

ls > test.txt & disown %1

This will do the same thing without all the extra cogs.

  • Thanks for your answer. The solution screen -dmS session_name sh -c 'command > test.txt; exec bash' (from the duplicate question) seems good too! – Basj May 20 '22 at 14:20
  • To me it seems that using screen logging feature instead of redirection might be helpful here too: screen -L will put all displayed stuff into screenlog.0 instead. This might be useful and can be combined with -dmS features. – smido May 30 '23 at 12:24