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?
ls > text.txt
) exits. But so does the input to yourtest.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 intest.txt
, or are you satisfied with just the output of thels
command ending up there, and simply want to have an open screen session available? – AdminBee May 20 '22 at 10:38screen -dmS mysession bash -c 'ls -laW > test.txt; exec bash'
:ls -laW
does an error, and we can see it when doingscreen -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