Recent

Author Topic: [SOLVED] TProcess hangs on Linux  (Read 644 times)

Ludan

  • New Member
  • *
  • Posts: 11
[SOLVED] TProcess hangs on Linux
« on: October 10, 2025, 08:30:39 pm »
Tried to use TProcess on ArchLinux but it hangs for dir command or ls or df , it waits for the user to press Enter/Return . Other commands like ncdu or nnn or ranger fail to launch.
Code: Pascal  [Select][+][-]
  1. Program tprc;
  2. uses sysutils, process;
  3. var aProc : TProcess;
  4. begin
  5.         aProc := TProcess.Create(nil);
  6.         aProc.Executable := 'dir';
  7.         aProc.Execute;
  8.         aProc.Free;
  9. end.

Used FPC version 3.2.2

I'm new to TProcess so maybe I'm doing something wrong.

I would also be interested how to launch builtin bash functions like history, or aliases or escaping aliases.
Also tried in windows 7 VM the dir command but did not work.
Launching graphical programs worked, like mate-calc in Linux or notepad / calc in windows.
Also worth noting is that when piping the output of dir into cat or cut it worked and did not expect a new line from the user:
./tprc | cat
./tprc | cut -f 1 -d '.'

Any constructive answer is much appreciated.
« Last Edit: October 10, 2025, 11:55:36 pm by Ludan »

zeljko

  • Hero Member
  • *****
  • Posts: 1792
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TProcess hangs on Linux
« Reply #1 on: October 10, 2025, 09:56:19 pm »
See TProcess.Options, also in process unit you have functions
Code: Pascal  [Select][+][-]
  1. RunCommandIndir(const curdir:TProcessString;const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; out exitstatus:integer; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):integer;
  2. function RunCommandIndir(const curdir:TProcessString;const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;
  3. function RunCommand(const exename:TProcessString;const commands:array of TProcessString;out outputstring:string; Options : TProcessOptions = [];SWOptions:TShowWindowOptions=swoNone):boolean;
  4.  
  5. function RunCommandInDir(const curdir,cmdline:TProcessString;out outputstring:string):boolean; deprecated;
  6. function RunCommand(const cmdline:TProcessString;out outputstring:string):boolean; deprecated;
.... so look there what fits your needs.   
EDIT: fixed code identation, sorry....
« Last Edit: October 10, 2025, 10:07:10 pm by zeljko »

Fred vS

  • Hero Member
  • *****
  • Posts: 3716
    • StrumPract is the musicians best friend
Re: TProcess hangs on Linux
« Reply #2 on: October 10, 2025, 10:38:22 pm »
Hello Ludan.

Indeed (and strange), using only "dir" needs some key press.

There is a workaround, using dir custom parameters:

Code: Pascal  [Select][+][-]
  1. Program tprc;
  2.     uses sysutils, process;
  3.     var aProc : TProcess;
  4.     begin
  5.             aProc := TProcess.Create(nil);
  6.             aProc.Executable := 'dir';
  7.             aProc.Parameters.add('-l');
  8.             aProc.Execute;
  9.             aProc.Free;
  10.     end.

Note also that it is better to use the full path of the binary:
Code: Pascal  [Select][+][-]
  1. aProc.Executable := '/usr/bin/dir';
« Last Edit: October 10, 2025, 10:45:02 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12531
  • FPC developer.
Re: TProcess hangs on Linux
« Reply #3 on: October 10, 2025, 11:12:52 pm »
Or maybe execute it over the shell

Code: Pascal  [Select][+][-]
  1. var s : string;
  2.  
  3. runcommand('/bin/sh',['-c','dir'],s);
  4.  
  5.  
Same advise for the pipe stuff
« Last Edit: October 10, 2025, 11:19:06 pm by marcov »

Fred vS

  • Hero Member
  • *****
  • Posts: 3716
    • StrumPract is the musicians best friend
Re: TProcess hangs on Linux
« Reply #4 on: October 10, 2025, 11:15:44 pm »
Hello Ludan.

This works here, no more Enter key needed.
 
Code: Pascal  [Select][+][-]
  1.   Program tprc;
  2.     uses sysutils, process;
  3.     var aProc : TProcess;
  4.     begin
  5.             aProc := TProcess.Create(nil);
  6.             aProc.Executable := '/usr/bin/dir';
  7.             aProc.Execute;
  8.             aProc.WaitOnExit(10);  // Add this. On my system 10 is ok, maybe more is needed.
  9.             aProc.Free;
  10.     end.
« Last Edit: October 10, 2025, 11:19:26 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Thausand

  • Sr. Member
  • ****
  • Posts: 397
Re: TProcess hangs on Linux
« Reply #5 on: October 10, 2025, 11:28:07 pm »
I'm new to TProcess so maybe I'm doing something wrong.
zeljko give good hint. Make use RunCommand(InDir) more easy for use. Have not same control if compare TProcess. So have make pick what work.

Quote
I would also be interested how to launch builtin bash functions like history, or aliases or escaping aliases.
Builtin command is specific shell and have difference for all (have luck and all shell have common things).

bash internal is call:
Code: [Select]
$ bash -c command parameters
or have use full path bash (or other) shell.

Run internal is not same working if compare external command when use TProcess or RunCommand.

Code: Pascal  [Select][+][-]
  1.   // external
  2.   if RunCommand('ls',[],value) then writeln(value) else writeln('error');
  3.   writeln;
  4.   // internal
  5.   if RunCommand('bash',['-c','ls'],value) then writeln(value) else writeln('error');
  6.  
And when want alias for run then also is require interactive shell option -i
« Last Edit: October 10, 2025, 11:30:19 pm by Thausand »

Fred vS

  • Hero Member
  • *****
  • Posts: 3716
    • StrumPract is the musicians best friend
Re: TProcess hangs on Linux
« Reply #6 on: October 10, 2025, 11:29:47 pm »
Hello Ludan.

OK, the last.

This one works too:

Code: Pascal  [Select][+][-]
  1. Program tprc;
  2.     uses sysutils, process;
  3.     var aProc : TProcess;
  4.     begin
  5.             aProc := TProcess.Create(nil);
  6.             aProc.Executable := '/usr/bin/dir';
  7.             aProc.Options := aProc.Options + [poWaitOnExit];
  8.             aProc.Execute;
  9.             aProc.Free;
  10.     end.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Ludan

  • New Member
  • *
  • Posts: 11
Re: TProcess hangs on Linux
« Reply #7 on: October 10, 2025, 11:42:05 pm »
Hello Ludan.

This works here, no more Enter key needed.
 
Code: Pascal  [Select][+][-]
  1.   Program tprc;
  2.     uses sysutils, process;
  3.     var aProc : TProcess;
  4.     begin
  5.             aProc := TProcess.Create(nil);
  6.             aProc.Executable := '/usr/bin/dir';
  7.             aProc.Execute;
  8.             aProc.WaitOnExit(10);  // Add this. On my system 10 is ok, maybe more is needed.
  9.             aProc.Free;
  10.     end.

That works, it does not need to be more than 10, it works with 1 also, it also works if you provide no parameters like aProc.WaitOnExit() or even aProc.WaitOnExit , but it only hangs if it has zero (0) as a parameter.
If aProc.WaitOnExit is not invoked it seems to be set to zero by default which seems wrong (for my case at least).
But that is a solution!
Thank you so much!

Fred vS

  • Hero Member
  • *****
  • Posts: 3716
    • StrumPract is the musicians best friend
Re: TProcess hangs on Linux
« Reply #8 on: October 10, 2025, 11:46:42 pm »
Hello Ludan.

This works here, no more Enter key needed.
 
Code: Pascal  [Select][+][-]
  1.   Program tprc;
  2.              ...
  3.               aProc.WaitOnExit(10);  // Add this. On my system 10 is ok, maybe more is needed.
  4.             ...
  5.     end.
Thank you so much!

You are welcome and I am happy that it works for you.  ;D

But, I prefer the solution from my previous post Reply #6  with:
Code: Pascal  [Select][+][-]
  1. aProc.Options := aProc.Options + [poWaitOnExit];
« Last Edit: October 10, 2025, 11:49:58 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Ludan

  • New Member
  • *
  • Posts: 11
Re: TProcess hangs on Linux
« Reply #9 on: October 10, 2025, 11:51:12 pm »
Or maybe execute it over the shell

Code: Pascal  [Select][+][-]
  1. var s : string;
  2.  
  3. runcommand('/bin/sh',['-c','dir'],s);
  4.  
  5.  
Same advise for the pipe stuff

That also works and it simpler.
Thank you.

Ludan

  • New Member
  • *
  • Posts: 11
Re: [SOLVED] TProcess hangs on Linux
« Reply #10 on: October 10, 2025, 11:56:32 pm »
Thank you all for replying.
I consider this solved.

 

TinyPortal © 2005-2018