[fpc-pascal] Re: Linux: How to execute a program, but not to wait until finishes

Lukasz Sokol el.es.cr at gmail.com
Mon Jul 4 17:21:32 CEST 2011


On 04/07/2011 08:14, fred f wrote:
> Hi all,
> 
> How can I execute an external program, but to let my code continue.
> 
> This doesn't work as I want and waits for finishing called app:
> 
>         ExecuteProcess (MyPathToExe, ' &');
> 
That would only work if you do 

ExecuteProcess('/bin/bash -c /my/path/to/exe &'); // (probably...?)

:) the & is shell/command line interpreter command only.

On the more serious note:

I think you need to use fork http://www.freepascal.org/docs-html/rtl/oldlinux/fork.html
and then one of the exec* procedures http://www.freepascal.org/docs-html/rtl/index-8.html#SECTIONE
(search for exec on this page, there is 6 of them related to linux
  - Execl, Execle,Execlp,Execv,Execve,Execvp - that _do_not_return_ when called! that's 
why you need to use Fork())

(Pseudopascalcode)

PID := Fork();
case PID of   // parent process goes on bypassing the below block

  0: begin

       Execl(MyPathToExe); /// forked child will not return, WILL NOT RETURN! on success
			   // so in case the exec* fails, the forked child needs to exit 
			   // gracefully.
       exit;		   // 
     end;

  else
    // here PID is either of the parent - check with GetPPid as Fork() man page says
    // 
    // or -1 if there were errors 
end;

(Somebody, correct me if I'm wrong)

HTH,
L.

> Thanks.
> 
> Bye, Fred





More information about the fpc-pascal mailing list