Forum > Linux

[SOLVED] Problem running external program under Linux

<< < (2/7) > >>

lucamar:

--- Quote from: maurobio on July 29, 2021, 01:12:59 pm ---But as far as I can see, a console program cannot be executed from a GUI application under Linux in the same way it can be under Windows.
--- End quote ---

It can, only Linux doesn't need a "command prompt" or a console window to execute it, which is why if you want that you have to run the program under a terminal emulator. That is, you "run" the terminal and pass to it, as parameters, the program you want it to execute.


--- Quote ---How do I 'execute a terminal emulator explicitly', as suggested by @rsz? Could an example be provided?
--- End quote ---

Basically, like this (just Q&D example, mind!):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; {$mode objfpc}{$H+} uses  Process;; var  AProcess: TProcess; begin  AProcess := TProcess.Create(Nil);  try    AProcess.Executable := DetectXTerm;    AProcess.Parameters.Add('counter');    AProcess.Parameters.Add('lines.txt');    AProcess.Options := AProcess.Options + [poWaitOnExit];    AProcess.Execute;  finally    AProcess.Free;  end;end.

--- Quote ---Also, I would appreciate a tip on how to check ExitStatus as suggested by @lucamar.
--- End quote ---

Well, you have to know the possible codes to diagnose the problem but at a basic level you check against zero, as you do with ExitCode, and "barf" out if it failed.

sstvmaster:

--- Quote from: maurobio on July 28, 2021, 11:48:28 pm ---... but it is a relatively straightforward program which executes a C program, passing the name of text file as its only argument and counts the lines in the file ...

--- End quote ---

Why don't you count the lines from this file youself?

maurobio:
@lucamar,

Unfortunately, it still didn't work. DetectXTerm returns 'x-terminal-emulator', which bash claims does not exist on my system (shouldn't it return LXTerminal, since I am running Lubuntu?). My program just stalls as before.

@sstvmaster.

To count lines in a file is not really my problem; the 'counter' program is just a dummy sample program which emulates the behaviour of the 'true' program I am attempting to run from my GUI (that is, a console program which reads a file passed as argument and waits for user commands until a 'quit' command is typed).

Again, thank you!

With best wishes,

MarkMLl:

--- Quote from: lucamar on July 29, 2021, 02:44:49 pm ---It can, only Linux doesn't need a "command prompt" or a console window to execute it, which is why if you want that you have to run the program under a terminal emulator. That is, you "run" the terminal and pass to it, as parameters, the program you want it to execute.

--- End quote ---

It will probably never need a window (i.e. xterm or equivalent) to run a child program, unless that program requires that the DISPLAY or one of the XDG_ shell variables etc. to be set up sensibly. I know that sounds silly for a command line program, but consider the case of the Subversion client or Sudo which can tell the desktop environment to display a prettified password prompt dialog(ue).

It might need a shell if, for some reason, the command being passed includes redirection/piping or variable substitution etc., there's usually ways of working round this.

If termination is failing, it is likely to be because the parent is not waiting for activity to stop, or because the child is expecting something like \r\n and is only seeing \r.

MarkMLl

avra:
Maybe it could help to take a look at https://bitbucket.org/avra/ct4laz. That is a GUI application which executes 7zip command line executable with parameters and processes it's output, while working on both Windows and Linux.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version