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, ' &');Thanks.
Bye, Fred

Mailing lists / fpc-pascal / 2011-07
19 messages · 9 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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, ' &');Thanks.
Bye, Fred
The following will work if you put the 'process' unit in your uses clause:
With TProcess.Create(Nil) do
try
CommandLine:=MyPathToExe;
Execute;
finally
Free;
end;Michael.
How can I execute an external program, but to let my code continue. This doesn't work as…
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
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.
That's what executeprocess and the other ways do for you.
You've snipped a ' :)' that was in the next line :)
A day without learning something new, is a day lost ;)
A 4th way is using TProcess, which in trunk now also has possibilities to specify arguments individually to avoid certain quoting problems.
I like this one.
OK.
:)
L.
first of all, thanks for answers, but
> fpsystem('/my/path/to/exe &');
works fine and it does what I want, but when my application ends then the started app over fpsystem is closed as well.
Thanks in advance.
F.
Try Michael's suggestion. It works, even on other platforms.
Hi,
use this code, which starts itself, but when you close first started the second ends as well, what I don't want.
with TProcess.Create (Application) do begin
CommandLine := Application.ExeName + ' &';
Execute;
Free;
end;Thanks.
F.
If I remember correctly this is simply the way Unix behaves. If the parent process terminates all children are terminated as well. I don't know how one can circumvent this.
Regards,
Sven
sounds like maybe they want "detach" but that won't work if they require to interface with the app...
If I remember correctly this is simply the way Unix behaves. If the parent process…
In our previous episode, Sven Barth said:
If I remember correctly this is simply the way Unix behaves. If the parent process terminates all children are terminated as well. I don't know how one can circumvent this.
Probably you have to walk to some of the job control related commands which are also done during daemonization. Essentially you want to detach from parent, but maybe not become a full daemon (without stdin-out, specially when they are piped)
If I remember correctly this is simply the way Unix behaves. If the parent process…
Orphans are reparented to PID 1, the init process.
Simple demonstration in a terminal
$ sh
$ gedit &
$ ps -lYou see that gedit is parented to the sh just launched.
$ exit
$ ps -lgedit is still running but now parented to pid 1.
Ludo
In our previous episode, Sven Barth said: Probably you have to walk to some of the job…
Marco van de Voort wroteProbably you have to walk to some of the job control related commands which are also done during daemonization. Essentially you want to detach from parent, but maybe not become a full daemon (without stdin-out, specially when they are piped)
Looking at what nohup does could be a good start.
Jonas
first of all, thanks for answers, but works fine and it does what I want, but when my…
with TProcess.Create(nil) do begin
CommandLine := '/my/path/to/exe';
Execute;
Free;
end;works fine. The child continues after the parent dies.
Ludo
I don't know on which system you tried, but on my Ubuntu 11.04 64bit it dies with parent one.
Looking at what nohup does could be a good start. Jonas
Thanks for nohup tip which works fine, but there is another point what makes me trouble.
My "mother" application has opened some files when calls its daughter over nohup. But the daugther gets all files opened to its processid.
when I check it with "ls -l /proc/PROCESSID/fd" I see all files linked to daughter when mother has closed them all. This raises me problems with next start of mother where daughter is still alive.
How to get rid of this never ending story.
Thanks in advance.
F.
I don't know on which system you tried, but on my Ubuntu 11.04 64bit it dies with parent…
Ubuntu 10.04 64 bit. Attached the test program. Compile and launch
$ ./testshell your-programYou'll find your-program in ps -l parented to PID 1.
Ludo
BTW fpc svn 17615
Tried this also on solaris x86 and ubuntu 11.04 32 bit.
Ludo
I don't know on which system you tried, but on my Ubuntu 11.04 64bit it dies with parent…
I only see this behavior when running the program from lazarus. Lazarus and/or gdb seem to clean up orphans when exiting the parent process.
Ludo