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

Marco van de Voort marcov at stack.nl
Mon Jul 4 19:05:03 CEST 2011


In our previous episode, Lukasz Sokol said:
> 
> ExecuteProcess('/bin/bash -c /my/path/to/exe &'); // (probably...?)

First, the binary name is always separate, there are minimal two parameters.

Moverover I would use the separate syntax, or use "" to signal that the
/commandline my...  & belongs together.

So either (the better solution)

  executeprocess('/bin/bash',['-c','/my/path/to/exe &']);

or the old syntax:

 executeprocess('/bin/bash,' -c "/my/path/to/exe &"');

or using more unixy routines:

fpsystem('/my/path/to/exe &');

the old syntax has as disadvantage that in more complex cases gets problems
with nested quoting. The last syntax uses the default system shell, which
might not be bash (and bash might not be in /bin everywhere, when installed)

A 4th way is using TProcess, which in trunk now also has possibilities to
specify arguments individually to avoid certain quoting problems.

> 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())

That's what executeprocess and the other ways do for you.




More information about the fpc-pascal mailing list