Recent

Author Topic: [SOLVED] TProcess returns nothing  (Read 616 times)

Zvoni

  • Sr. Member
  • ****
  • Posts: 423
[SOLVED] TProcess returns nothing
« on: September 10, 2020, 11:55:37 pm »
Hi Folks,

Mojave 10.14.6
Laz/FPC fixes (2.0.11/3.2.1)

How do i read the output from stdout/stdErr called through TProcess on MacOS?
On Linux no problems, on Mac: Nada, njet.

i'm using the usual Process-Options (NoConsole,UsePipes, ErrtoOutPut) which work on Linux.
I'm using the sample from the Wiki-page (which i have running successfully on Linux)

the command-line is actually "which ffprobe" (curiously: executing ffprobe in terminal on Mac works as i'm used to from Linux, using it as the executable in TProcess returns "program not found" or something.) So It's not a question if ffprobe is in PATH, since directly in the terminal it works.
If i use the full path to ffprobe as my executable for TProcess it works, so i wanted to use a "which ffprobe" to return the path, but.......
« Last Edit: September 11, 2020, 07:19:39 pm by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1062
  • Former Delphi 1-7, 10.2 User
Re: TProcess returns nothing
« Reply #1 on: September 11, 2020, 03:43:00 am »
TProcess does not inherit the environment variables (eg PATH) because it is not a shell; that is why you get command not found.

This is what I do on macOS (Mojave):

Code: Pascal  [Select][+][-]
  1. Program DemoProcess;
  2.  
  3. uses
  4.   Classes, SysUtils, Process;
  5.  
  6. var
  7.   AProcess: TProcess;
  8.   AStringList: TStringList;
  9.  
  10. begin
  11.   AProcess := TProcess.Create(nil);
  12.   AProcess.Executable := '/bin/sh';
  13.   AProcess.Parameters.Add('-c');
  14.   AProcess.Parameters.Add('"date"');
  15.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes, poStderrToOutPut];
  16.   AProcess.Execute;
  17.  
  18.   AStringList := TStringList.Create;
  19.   AStringList.LoadFromStream(AProcess.Output);
  20.   AStringList.SaveToFile('output.txt');
  21.  
  22.   AStringList.Free;
  23.   AProcess.Free;
  24. end.

output.txt contains "Fri 11 Sep 2020 11:41:38 AEST".

Note: poNoConsole is a Win32 only option.
o Lazarus v2.1.0 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 858
Re: TProcess returns nothing
« Reply #2 on: September 11, 2020, 08:14:21 am »
hello,
the command-line is actually "which ffprobe" (curiously: executing ffprobe in terminal on Mac works as i'm used to from Linux, using it as the executable in TProcess returns "program not found" or something.)

instead of using which to search the path of ffprobe you can try to use the FindDefaultExecutablePath function from FileUtil unit.

Friendly, J.P
« Last Edit: September 11, 2020, 08:16:24 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 804
Re: TProcess returns nothing
« Reply #3 on: September 11, 2020, 09:13:33 am »
"which" exists both as a shell built-in and as an external program. On macOS, it is not installed as an external program by the system. As a result trying to start "which" as a separate process fails. It's the same as trying to execute "set" as an external process. You can use Trevor's workaround, but you will have to remove the "-c" because of the fact that it's not an external program, but a built-in command of the shell.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1062
  • Former Delphi 1-7, 10.2 User
Re: TProcess returns nothing
« Reply #4 on: September 11, 2020, 10:29:00 am »
"which" exists both as a shell built-in and as an external program. On macOS, it is not installed as an external program by the system.
My Mojave system  has a /usr/bin/which external program with same same date stamp as the uucp binary in the same directory, so it's unlikely to have been installed by a dev tool. My Leopard system also has a /usr/bin/which.
o Lazarus v2.1.0 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

Zvoni

  • Sr. Member
  • ****
  • Posts: 423
Re: TProcess returns nothing
« Reply #5 on: September 11, 2020, 05:49:46 pm »
Reporting back:
no "-c" --> /bin/sh which ffprobe results in: cannot execute binary file --> no cigar
FindDefaultExecutablePath returns empty string (no Basedir specified)-->  --> no cigar
/bin/sh -c which ffprobe executes which, but disregards anything after it (options/parameters): returns "usage which [-as] program" --> no cigar

Ideas?
I admit: I don't have a clue about MacOS, just trying to get a program running there
« Last Edit: September 11, 2020, 05:51:24 pm by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Zvoni

  • Sr. Member
  • ****
  • Posts: 423
Re: TProcess returns nothing
« Reply #6 on: September 11, 2020, 07:19:19 pm »
Solved!
But Boy, what a hassle.

Code: Pascal  [Select][+][-]
  1. Function GetPath:String;
  2. Var
  3.   sCommand:String;
  4.   oProcess:TProcess;
  5.   oStrings:TStringList;
  6. Begin
  7.   sCommand:='sh -c '+QuotedStr('which ffprobe');
  8.   oProcess:=TProcess.Create(nil);
  9.   oProcess.Options:=[poWaitOnExit,poUsePipes,poStderrToOutPut];
  10.   oProcess.CommandLine:=sCommand;
  11.   oProcess.Execute;
  12.   oStrings:=TStringList.Create(nil);
  13.   oStrings.LoadFromStream(oProcess.OutPut);
  14.   Result:=Trim(oStrings.Text);
  15.   oStrings.Free;
  16.   oProcess.Free;
  17. End;
  18.  

Funny enough: couldn't get it to work with Executable and Parameters, only with (deprecated) CommandLine (and yes: i commented it out beforehand)
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1062
  • Former Delphi 1-7, 10.2 User
Re: [SOLVED] TProcess returns nothing
« Reply #7 on: September 12, 2020, 02:47:00 am »
No need to use the deprecated CommandLine. This works for me:

Code: Pascal  [Select][+][-]
  1. Program DemoProcess;
  2.  
  3. uses
  4.   Classes, SysUtils, Process;
  5.  
  6. var
  7.   AProcess: TProcess;
  8.   AStringList: TStringList;
  9.  
  10. begin
  11.   AProcess := TProcess.Create(nil);
  12.  
  13.   AProcess.Executable := '/bin/sh';
  14.   AProcess.Parameters.Add('-c');
  15.   AProcess.Parameters.Add('which date');
  16.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes, poStderrToOutPut];
  17.   AProcess.Execute;
  18.  
  19.   AStringList := TStringList.Create;
  20.   AStringList.LoadFromStream(AProcess.Output);
  21.   WriteLn(AStringList.Text);
  22.  
  23.   AStringList.Free;
  24.   AProcess.Free;
  25. end.

produces:

Quote
$ ./demoprocess
/bin/date
o Lazarus v2.1.0 r63871, FPC v3.3.1 r47164, macOS 10.14.6, Xcode 11.3.1
o Lazarus v2.1.0 r64160, FPC v3.3.1 Nov 27 21:16:31, macOS 11.0.1 (aarch64), Xcode 12.2
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 amd64 (VMware VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 20.04 (PD VM)

 

TinyPortal © 2005-2018