Recent

Author Topic: TProcess ExitCode?  (Read 1557 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
TProcess ExitCode?
« on: November 16, 2024, 03:17:05 pm »
Hello,
under Linux, using this (bash) script with 755 permissions:

Code: Text  [Select][+][-]
  1. #!/bin/bash
  2.  
  3. echo "Input: $1";
  4. exit $1;
  5.  
  6.  

if I execute in a console:

Code: Pascal  [Select][+][-]
  1.  
  2. $ ./test-script.sh 0;
  3. Input: 0
  4. $ echo $?;
  5. 0
  6. $ ./test-script.sh 1;
  7. Input: 1
  8. $ echo $?;
  9. 1
  10. $ bash test-script.sh 0;
  11. Input: 0
  12. $ echo $?;
  13. 0
  14. $ bash test-script.sh 1;
  15. Input: 1
  16. $ echo $?;
  17. 1
  18.  
  19.  

but if I run the following program:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   SysUtils, process;
  5.  
  6. var
  7.   p: TProcess;
  8. begin
  9.   p := TProcess.Create(nil);
  10.   try
  11.    
  12.     p.Executable := './test-script.sh';
  13.     p.Options := [TProcessOption.poWaitOnExit];
  14.    
  15.     p.Parameters.Add('');
  16.    
  17.     p.Parameters[0] := '0';
  18.     p.Execute();
  19.     Writeln(Format('Exit code: %d', [p.ExitCode]));
  20.    
  21.     p.Parameters[0] := '1';
  22.     p.Execute();
  23.     Writeln(Format('Exit code: %d', [p.ExitCode]));
  24.    
  25.         finally
  26.     FreeAndNil(p);
  27.         end;
  28.  
  29.   p := TProcess.Create(nil);
  30.   try
  31.    
  32.     p.Executable := '/bin/bash';
  33.     p.Options := [TProcessOption.poWaitOnExit];
  34.    
  35.     p.Parameters.Add('test-script.sh');
  36.     p.Parameters.Add('');
  37.    
  38.     p.Parameters[1] := '0';
  39.     p.Execute();
  40.     Writeln(Format('Exit code: %d', [p.ExitCode]));
  41.    
  42.     p.Parameters[1] := '1';
  43.     p.Execute();
  44.     Writeln(Format('Exit code: %d', [p.ExitCode]));
  45.    
  46.   finally
  47.     FreeAndNil(p);
  48.   end;
  49. end.
  50.  

the result is:

Code: Pascal  [Select][+][-]
  1. $ ./project1
  2. Input: 0
  3. Exit code: 0
  4. Input: 1
  5. Exit code: 0
  6. Input: 0
  7. Exit code: 0
  8. Input: 1
  9. Exit code: 0
  10.  

Is this correct? What am I missing in TProcess usage?
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: TProcess ExitCode?
« Reply #1 on: November 16, 2024, 03:30:07 pm »
Update:

if I ask for
Code: Pascal  [Select][+][-]
  1. ExitStatus
instead of
Code: Pascal  [Select][+][-]
  1. ExitCode
output becomes

Code: Pascal  [Select][+][-]
  1. $ ./project1
  2. Input: 0
  3. Exit code: 0
  4. Input: 1
  5. Exit code: 1
  6. Input: 0
  7. Exit code: 0
  8. Input: 1
  9. Exit code: 1
  10.  

Probably just the name ExitStatus refers to the linux exit code of the programs.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

zeljko

  • Hero Member
  • *****
  • Posts: 1690
    • http://wiki.lazarus.freepascal.org/User:Zeljan

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: TProcess ExitCode?
« Reply #3 on: November 17, 2024, 07:49:42 pm »
https://www.freepascal.org/docs-html/fcl/process/tprocess.exitcode.html

Thanks, I read the docs before posting here.
In the case of a process that terminates on it's own, ExitStatus is said has to coincide with ExitCode, but my examples seems is the opposite. I miss something for sure.
« Last Edit: November 17, 2024, 09:13:39 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

zeljko

  • Hero Member
  • *****
  • Posts: 1690
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TProcess ExitCode?
« Reply #4 on: November 18, 2024, 10:08:37 am »
But it says that on unix status code can be different than exit code.

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: TProcess ExitCode?
« Reply #5 on: November 18, 2024, 03:28:08 pm »
But it says that on unix status code can be different than exit code.

Quote
From ExitCode page:
ExitCode is the actual exit code of the process. On UNIX, this may differ from the ExitStatus value if the process was terminated by a signal: in that case ExitStatus is
the raw exit status as reported by one of the UNIX Wait command, and ExitCode is the exit code reported by the program.

From ExitStatus page:
ExitStatus contains the exit status as reported by the OS for the process when it stopped executing: Normally, this is the exit code of the process.
The value of this property is only meaningful when the process has finished executing. If it is not yet running then the value is -1. (it was zero in earlier versions of FPC)

I am sorry, maybe brain is not very efficient anymore, but these two explanations are not understandable by me. If someone can clarify I'd be grateful (applied to example posted originally).
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: TProcess ExitCode?
« Reply #6 on: November 18, 2024, 05:15:00 pm »
The exitcode says something about the process part of fpc finishing correctly (= 0)
The exitstatus is the real exitcode of the process itself, indicating the process may have exited with an error, which in turn gets propagated to Tprocess.exitstatus. You should examine both values:
1. exitcode for running Tprocess and indicates a problem with the fpc code
2. exitstatus to tell what the external program itself thinks it needs to tell you after it finished
There is nothing wrong with being blunt. At a minimum it is also honest.

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: TProcess ExitCode?
« Reply #7 on: November 18, 2024, 08:56:27 pm »
The exitcode says something about the process part of fpc finishing correctly (= 0)
The exitstatus is the real exitcode of the process itself, indicating the process may have exited with an error, which in turn gets propagated to Tprocess.exitstatus. You should examine both values:
1. exitcode for running Tprocess and indicates a problem with the fpc code
2. exitstatus to tell what the external program itself thinks it needs to tell you after it finished

Thanks Thaddy,
I would like to see some practical example to see this distinctions in place, if someone can suggest a code sample, it is welcome.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: TProcess ExitCode?
« Reply #8 on: November 18, 2024, 11:04:22 pm »
Well, I write one... O:-)
Suppose you write two programs - on a nix machine - in fpc, one the child:
Code: Pascal  [Select][+][-]
  1. program childproc;
  2. begin
  3.    halt(123);// max 255, simulate error condition or message
  4. end.
Code: Pascal  [Select][+][-]
  1. program parentproc;
  2. {$mode objfpc}
  3. uses
  4.   Process;
  5. var
  6.   AProcess: TProcess;
  7. begin
  8.   AProcess := TProcess.Create(nil);
  9. {$ifdef mswindows}
  10.   AProcess.Executable:= 'childproc.exe';
  11. {$else}
  12.   AProcess.Executable:= './childproc';
  13. {$endif}
  14.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  15.   AProcess.Execute;
  16.   writeln(AProcess.Exitcode);
  17.   writeln(AProcess.ExitStatus);
  18.   AProcess.Free;  
  19. end.


And you run that under TProcess, then the exitcode is 0, meaning the parent had no problems,  but the exitstatus is 123.......because the child reported that.
Except, as documented, under Windows, where they are equal and both report 123.
On Windows you get 0 for GetLastError(), <-- the winapi call, not GetLastOsError, if TProcess succeeded. I do not know why this was not implemented the same on Windows as on Linux, because you can achieve the same result.
But your question was about Linux.
« Last Edit: November 19, 2024, 06:37:47 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: TProcess ExitCode?
« Reply #9 on: November 19, 2024, 08:05:43 pm »
Well, I write one... O:-)

Than you very much Thaddy, the example you posted is crystal clear.

Still I don't understand why should AProcess.ExitCode be able to tell something about the parent* (AProcess is a pointer to a struct aimed to handle the child, but tells something about the parent? ), but now  is clear to me how to get out of the initial issue.

(*) and why this something is different on Windows, where there is still dad and son, but numbers are different.
« Last Edit: November 19, 2024, 08:07:26 pm by Чебурашка »
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Thaddy

  • Hero Member
  • *****
  • Posts: 16411
  • Censorship about opinions does not belong here.
Re: TProcess ExitCode?
« Reply #10 on: November 19, 2024, 08:35:05 pm »
Tprocess exitcode is like Papa: oh well it's OK to let them go to this party, but I have to check if they are home, not on time, but home.
The exitstatus is like son or daughter going out with Papa's permission and end up with a headache...(serious bug, they think) and at least two hours late...
Papa looks and thinks: Oh, well, back to bed, they are safely home. (nothing serious happened, will talk to them tomorrow)

Been there, done it, from both sides...as father and son.

And the same is the case here.. ;D ;D

Clear?

This is relevant, Same thing.

In the case of the apocalypse (crash) both parent and child are doomed anyway.
« Last Edit: November 19, 2024, 09:02:17 pm by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

 

TinyPortal © 2005-2018