maybe try this? when thinking outside the screen program 1. start up screen with an expectation there's more to do when screen quits, like check a file with a result
screen && cat /tmp/exit_code # if your intent is exit screen normally, the use AND "&&"... otherise if your screen session is killed abnormally from some other terminal, you might need to use OR "||" 2. while inside screen, expect that the final line of your script will ALWAYS be storage mechanism for the previous command cat /tmp/nosuchfile # last desired command in script (i'm intentionally getting an exit code code here other than zero) echo $? > /tmp/exit_code # storing exit status of that last command, which is the final command in script screen -X quit # exit screen either manually, or via script 3. Once screen exits you should see the exit status of that last desired command from script ------------------- [screen is terminating] 1 -------------------- does that help you? On Wed, May 8, 2024 at 2:41 PM Alan D. Salewski <salew...@att.net> wrote: > On 2024-05-07 10:24:49, Marek Lukács <ma...@lukacs.cz> spake thus: > >Is it possible to capture last exit status code after screen finished (eg. > >environment variable $SCREEN_EXIT_CODE), or copy last status code to > screen' > >s exit status code, so I can check it using $? ? > > Hi Marek, > > I'm not aware of any way to cause a program's exit status to be > reflected in the exit status of 'screen' itself. But even if that > were possible, it would probably be less reliable than you would > want, because the ability to detect an error by the status code > would then depend on the order in which subprocesses exit within > screen. > > E.g., if a given screen session runs two subprocesses, and the first > one exits with a non-zero (error) status and the other exits with a > zero (success) status a moment later, then having the "last exit > status code" reflected in the exit status of the screen process > would still mask the error of the subprocess that failed. > > A different approach would be to use wrapper scripts that write > their exit statuses to some well-known location in the file > system. Rather than have screen launch your program(s) directly, it > would instead launch a corresponding wrapper script: > > #!/bin/sh > /path/to/some/program "$@" > printf '%s\n' "$?" >> /path/to/estat > > To make that approach as reliable as possible, it would be best to > create '/path/to/estat' prior to launching the subprocess in the > screen session. That would allow you to detect "file system full" or > situations in which there are inadequate persmissions to write the > exit status file. > > Best regards, > -Al > > -- > a l a n d. s a l e w s k i > ads@salewski.email > salew...@att.net > https://github.com/salewski > >