Re: Inner exit status code

joshebosh <[email protected]> Wed, 8 May 2024 16:03:18 -0500
Newsgroups gmane.comp.gnu.screen.user
Message-ID <CAAQhn7mkTZEiRWfFn3NxDWksh8M=C22HV32Z3_7W+8hoJ5eyTw@mail.gmail.com>
--0000000000007af2f80617f7a23a
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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=E2=80=AFPM Alan D. Salewski <[email protected]> =
wrote:

> On 2024-05-07 10:24:49, Marek Luk=C3=A1cs <[email protected]> spake thus:
> >Is it possible to capture last exit status code after screen finished (e=
g.
> >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
> [email protected]
> [email protected]
> https://github.com/salewski
>
>

--0000000000007af2f80617f7a23a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">maybe try this?<div>when thinking outside the screen progr=
am</div><div><br></div><div>1. start up screen with an expectation there&#3=
9;s more to do when screen quits, like check a file with a result</div><div=
><br></div><div>screen &amp;&amp; cat /tmp/exit_code=C2=A0 =C2=A0 # if your=
 intent is exit screen normally, the use AND &quot;&amp;&amp;&quot;... othe=
rise if your screen session is killed abnormally from some other terminal, =
you might need to use=C2=A0 OR &quot;||&quot;<br></div><div><br></div><div>=
2. while inside screen, expect that the final line of your script will ALWA=
YS be storage mechanism for the previous command</div><div><br></div><div>c=
at /tmp/nosuchfile=C2=A0 =C2=A0 =C2=A0# last desired command in script (i&#=
39;m intentionally=C2=A0getting an exit code code here other than zero)<br>=
</div><div>echo $? &gt; /tmp/exit_code=C2=A0 =C2=A0 =C2=A0 =C2=A0 # storing=
 exit status of that last command, which is the final command in script</di=
v><div>screen -X quit=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # exi=
t screen either manually, or via script</div><div><br></div><div>3. Once sc=
reen exits you should see the exit status of that last desired command from=
 script</div><div>-------------------</div>[screen is terminating]<br><div>=
1=C2=A0<br></div><div>--------------------</div><div><br></div><div>does th=
at help you?</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cla=
ss=3D"gmail_attr">On Wed, May 8, 2024 at 2:41=E2=80=AFPM Alan D. Salewski &=
lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt; wrote:<br><=
/div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left:1px solid rgb(204,204,204);padding-left:1ex">On 2024-05-07 10:24:=
49, Marek Luk=C3=A1cs &lt;<a href=3D"mailto:[email protected]" target=3D"_bla=
nk">[email protected]</a>&gt; spake thus:<br>
&gt;Is it possible to capture last exit status code after screen finished (=
eg.<br>
&gt;environment variable $SCREEN_EXIT_CODE), or copy last status code to sc=
reen&#39;<br>
&gt;s exit status code, so I can check it using $? ?<br>
<br>
Hi Marek,<br>
<br>
I&#39;m not aware of any way to cause a program&#39;s exit status to be <br=
>
reflected in the exit status of &#39;screen&#39; itself. But even if that <=
br>
were possible, it would probably be less reliable than you would <br>
want, because the ability to detect an error by the status code <br>
would then depend on the order in which subprocesses exit within <br>
screen.<br>
<br>
E.g., if a given screen session runs two subprocesses, and the first <br>
one exits with a non-zero (error) status and the other exits with a <br>
zero (success) status a moment later, then having the &quot;last exit <br>
status code&quot; reflected in the exit status of the screen process <br>
would still mask the error of the subprocess that failed.<br>
<br>
A different approach would be to use wrapper scripts that write <br>
their exit statuses to some well-known location in the file <br>
system. Rather than have screen launch your program(s) directly, it <br>
would instead launch a corresponding wrapper script:<br>
<br>
=C2=A0 =C2=A0 =C2=A0#!/bin/sh<br>
=C2=A0 =C2=A0 =C2=A0/path/to/some/program &quot;$@&quot;<br>
=C2=A0 =C2=A0 =C2=A0printf &#39;%s\n&#39; &quot;$?&quot; &gt;&gt; /path/to/=
estat<br>
<br>
To make that approach as reliable as possible, it would be best to <br>
create &#39;/path/to/estat&#39; prior to launching the subprocess in the <b=
r>
screen session. That would allow you to detect &quot;file system full&quot;=
 or <br>
situations in which there are inadequate persmissions to write the <br>
exit status file.<br>
<br>
Best regards,<br>
-Al<br>
<br>
-- <br>
a l a n=C2=A0 =C2=A0d.=C2=A0 =C2=A0s a l e w s k i<br>
[email protected]<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a><=
br>
<a href=3D"https://github.com/salewski" rel=3D"noreferrer" target=3D"_blank=
">https://github.com/salewski</a><br>
<br>
</blockquote></div>

--0000000000007af2f80617f7a23a--