Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-07

Linux: How to execute a program, but not to wait until finishes

19 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

fred f

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

Unknown author
to fred f
fred f wrote 7 lines
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, ' &');

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.

Lukasz Sokol
to fred f: How can I execute an external program, but to let my code continue. This doesn't work as…
fred f wrote 8 lines
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
Marco van de Voort
to Lukasz Sokol

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.

Show quoted text 5 lines
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.

Lukasz Sokol
to Marco van de Voort
Marco van de Voort wrote 8 lines
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.

You've snipped a ' :)' that was in the next line :)

Show quoted text 20 lines
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 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.

Show quoted text 11 lines
> 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.

OK.

:)

L.

fred f
to Lukasz Sokol

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.

fred f
to leledumbo

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.

leledumbo wrote 9 lines
Try Michael's suggestion. It works, even on other platforms. -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Linux-How-to-execute-a-program-but-not-to-wait-until-finishes-tp4550251p4593043.html Sent from the Free Pascal - General mailing list . _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Sven Barth
to fred f
fred f wrote 10 lines
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;

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

waldo kitty
to Sven Barth
Sven Barth wrote 15 lines
On 19.07.2011 16:52, fred f wrote: > 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; 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.

sounds like maybe they want "detach" but that won't work if they require to interface with the app...

Marco van de Voort
to Sven Barth: 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)

Ludo Brands
to Sven Barth: If I remember correctly this is simply the way Unix behaves. If the parent process…
Show quoted text 28 lines
-----Message d'origine----- De : De la part de Sven Barth Envoyé : mardi 19 juillet 2011 20:14 À : Objet : Re: [fpc-pascal] Re: Linux: How to execute a program, but not towait until finishes On 19.07.2011 16:52, fred f wrote: > 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; 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.
Orphans are reparented to PID 1, the init process.
Simple demonstration in a terminal
$ sh
$ gedit &
$ ps -l

You see that gedit is parented to the sh just launched.

$ exit
$ ps -l

gedit is still running but now parented to pid 1.

Ludo

Ludo Brands
to fred f: 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

Show quoted text 23 lines
-----Message d'origine----- De : De la part de fred f Envoyé : vendredi 15 juillet 2011 16:14 À : FPC-Pascal users discussions Objet : Re: [fpc-pascal] Re: Linux: How to execute a program, but not to waituntil finishes 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. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
fred f
to Ludo Brands

I don't know on which system you tried, but on my Ubuntu 11.04 64bit it dies with parent one.

Ludo Brands wrote 40 lines
 with TProcess.Create(nil) do begin    CommandLine := '/my/path/to/exe';    Execute;    Free;  end; works fine. The child continues after the parent dies. Ludo > -----Message d'origine----- > De : > De la part de fred f > Envoyé : vendredi 15 juillet 2011 16:14 > À : FPC-Pascal users discussions > Objet : Re: [fpc-pascal] Re: Linux: How to execute a program, > but not to waituntil finishes > > > 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. > _______________________________________________ > fpc-pascal maillist  -   > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
fred f
to Jonas Maebe: Looking at what nohup does could be a good start. Jonas
Jonas Maebe wrote 9 lines
On 20 Jul 2011, at 08:13, Marco van de Voort wrote: > 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) Looking at what nohup does could be a good start.

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.

Ludo Brands
to fred 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-program

You'll find your-program in ps -l parented to PID 1.

Ludo

Show quoted text 59 lines
-----Message d'origine----- De : De la part de fred f Envoyé : mercredi 20 juillet 2011 12:19 À : FPC-Pascal users discussions Objet : Re: RE : [fpc-pascal] Re: Linux: How to execute a program, but not towaituntil finishes I don't know on which system you tried, but on my Ubuntu 11.04 64bit it dies with parent one. On Wed, Jul 20, 2011 at 9:53 AM, Ludo Brands wrote: >  with TProcess.Create(nil) do begin >    CommandLine := '/my/path/to/exe'; >    Execute; >    Free; >  end; > > > works fine. The child continues after the parent dies. > > Ludo > > >> -----Message d'origine----- >> De : >> De la part de fred f >> Envoyé : vendredi 15 juillet 2011 16:14 À : FPC-Pascal users >> discussions Objet : Re: [fpc-pascal] Re: Linux: How to execute a >> program, but not to waituntil finishes >> >> >> 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. >> _______________________________________________ >> fpc-pascal maillist  -   >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > _______________________________________________ > fpc-pascal maillist  -   > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Ludo Brands
to Ludo Brands

BTW fpc svn 17615

Tried this also on solaris x86 and ubuntu 11.04 32 bit.

Ludo

Show quoted text 79 lines
-----Message d'origine----- De : De la part de Ludo Brands Envoyé : mercredi 20 juillet 2011 12:49 À : 'FPC-Pascal users discussions' Objet : RE : RE : [fpc-pascal] Re: Linux: How to execute a program,but not towaituntil finishes Ubuntu 10.04 64 bit. Attached the test program. Compile and launch $ ./testshell your-program You'll find your-program in ps -l parented to PID 1. Ludo > -----Message d'origine----- > De : > De la part de fred f > Envoyé : mercredi 20 juillet 2011 12:19 > À : FPC-Pascal users discussions > Objet : Re: RE : [fpc-pascal] Re: Linux: How to execute a > program, but not towaituntil finishes > > > I don't know on which system you tried, but on my Ubuntu > 11.04 64bit it dies with parent one. > > On Wed, Jul 20, 2011 at 9:53 AM, Ludo Brands > wrote: > >  with TProcess.Create(nil) do begin > >    CommandLine := '/my/path/to/exe'; > >    Execute; > >    Free; > >  end; > > > > > > works fine. The child continues after the parent dies. > > > > Ludo > > > > > >> -----Message d'origine----- > >> De : > >> De la > part de fred f > >> Envoyé : vendredi 15 juillet 2011 16:14 À : FPC-Pascal users > >> discussions Objet : Re: [fpc-pascal] Re: Linux: How to execute a > >> program, but not to waituntil finishes > >> > >> > >> 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. > >> _______________________________________________ > >> fpc-pascal maillist  -   > >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal > >> > > > > _______________________________________________ > > fpc-pascal maillist  -   > > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
Ludo Brands
to fred f: 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

Show quoted text 50 lines
I don't know on which system you tried, but on my Ubuntu 11.04 64bit it dies with parent one. On Wed, Jul 20, 2011 at 9:53 AM, Ludo Brands wrote: >  with TProcess.Create(nil) do begin >    CommandLine := '/my/path/to/exe'; >    Execute; >    Free; >  end; > > > works fine. The child continues after the parent dies. > > Ludo > > >> -----Message d'origine----- >> De : >> De la part de fred f >> Envoyé : vendredi 15 juillet 2011 16:14 À : FPC-Pascal users >> discussions Objet : Re: [fpc-pascal] Re: Linux: How to execute a >> program, but not to waituntil finishes >> >> >> 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. >> _______________________________________________ >> fpc-pascal maillist  -   >> http://lists.freepascal.org/mailman/listinfo/fpc-pascal >> > > _______________________________________________ > fpc-pascal maillist  -   > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal