Using exec()

Discussion in 'Perl Misc' started by Bill H, Jan 14, 2009.

  1. Bill H

    Bill H Guest

    According to the perldoc exec() never returns when it is called, which
    is fine, but I can't see anywhere if the calling script will continue
    on or does it stop when you do the exec.

    The way I want to use it is I have a script that uses PDF::API to make
    a pdf based on user input, then using imagemagik it makes viewable
    jpgs out of the pages. What I want to do, is when the script has
    completed the above, I want to start another program that combines
    pdf's that have been made for latter viewing, but don't want the user
    to have to wait for it. Will the exec let me start another perl script
    but allow the calling script to finish up and exit as if it had never
    called an exec?

    Bill H
     
    Bill H, Jan 14, 2009
    #1
    1. Advertising

  2. Bill H <> writes:

    > According to the perldoc exec() never returns when it is called, which
    > is fine, but I can't see anywhere if the calling script will continue
    > on or does it stop when you do the exec.


    exec() replaces the current process by the new one. The only time the
    "calling script" will continue is if the exec() call fails. If exec
    succeeds there is no "calling script" anymore.

    > The way I want to use it is I have a script that uses PDF::API to make
    > a pdf based on user input, then using imagemagik it makes viewable
    > jpgs out of the pages. What I want to do, is when the script has
    > completed the above, I want to start another program that combines
    > pdf's that have been made for latter viewing, but don't want the user
    > to have to wait for it. Will the exec let me start another perl script
    > but allow the calling script to finish up and exit as if it had never
    > called an exec?


    Not on its own, no. You probably want some combination of fork() and
    exec(). What I personally tend to do is

    system("/some/function bla bla &") and die;

    to start a program in the background.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Jan 14, 2009
    #2
    1. Advertising

  3. Bill H <> wrote:
    >According to the perldoc exec() never returns when it is called,


    No. It never returns if the call was successfull. That is different.

    >which
    >is fine, but I can't see anywhere if the calling script will continue
    >on or does it stop when you do the exec.


    Well, if the call doesn't return, then how could the script continue?
    In any case, what happens behind the scenes is that your Perl code is
    being replaced(!) by the called external program and the process starts
    executing that new code.

    >Will the exec let me start another perl script
    >but allow the calling script to finish up and exit as if it had never
    >called an exec?


    No. exec() replaces the code and the old code is gone. I think you may
    be looking for system() instead of for exec().

    jue
     
    Jürgen Exner, Jan 14, 2009
    #3
  4. Bill H

    Bill H Guest

    On Jan 14, 12:12 pm, Jürgen Exner <> wrote:
    > Bill H <> wrote:
    > >According to the perldoc exec() never returns when it is called,

    >
    > No. It never returns if the call was successfull. That is different.
    >
    > >which
    > >is fine, but I can't see anywhere if the calling script will continue
    > >on or does it stop when you do the exec.

    >
    > Well, if the call doesn't return, then how could the script continue?
    > In any case, what happens behind the scenes is that your Perl code is
    > being replaced(!) by the called external program and the process starts
    > executing that new code.
    >
    > >Will the exec let me start another perl script
    > >but allow the calling script to finish up and exit as if it had never
    > >called an exec?

    >
    > No. exec() replaces the code and the old code is gone. I think you may
    > be looking for system() instead of for exec().
    >
    > jue


    Jurgen, from your response and the others (thank you), I see I have to
    use system() in some manner with fork() to get what I want.

    An example using exec()

    print "hello ";
    exec("perl otherscript.pm");
    print "world"; # World never prints

    An example using system()

    print "hello ";
    system("perl otherscript.pm");
    print "world "; # World prints when otherscript.pm is done

    What I need to do:

    print "hello ";
    not_exec_or_system("perl otherscript,pm");
    print "world"; # World prints immediately and otherscript.pm is
    running also

    Bill H
     
    Bill H, Jan 14, 2009
    #4
  5. Bill H

    Tim McDaniel Guest

    In article <>,
    Bill H <> wrote:
    >What I need to do:
    >
    >print "hello ";
    >not_exec_or_system("perl otherscript,pm");
    >print "world"; # World prints immediately and otherscript.pm is


    As Joost Diepenmaat <> wrote, you may be able to do
    that with

    print "hello ";
    system("perl otherscript.pm &");
    print "world"; # World prints immediately and
    # otherscript.pm is running also

    That presumes that your system's command interpreter uses terminal "&"
    to run a program in background: that works on Linux but not Windows
    cmd.exe. It also doesn't allow you to monitor the progress of
    otherscript.pm: neither the caller nor anything else can tell whether
    it's still running (except for heroic measures, or unless it itself
    signals its progress via a status file), or whether it ends with an
    error exit (even something like otherscript.pm not being an existing
    file, so it dies at once).

    If one of those conditions doesn't work for you, then you have to
    implement the "&" functionality yourself.
    - Method 1: use fork(). The parent does NOT wait() or
    waitpid() for the child. The child does an exec() of
    perl otherscript.pm.
    - Method 2: use fork(). The parent does NOT wait() or
    waitpid() for the child. The child does a system() of
    perl otherscript.pm. The child examines the return value of
    system() to see whether it was a "crash on takeoff", or whether
    there was a non-zero exit status, or a normal run, and does whatever
    seems indicated (log an error, e-mail someone, whatever). The child
    ends with an exit().

    --
    Tim McDaniel,
     
    Tim McDaniel, Jan 14, 2009
    #5
  6. Bill H <> wrote:
    >Jurgen, from your response and the others (thank you), I see I have to
    >use system() in some manner with fork() to get what I want.


    Well, maybe.

    >What I need to do:
    >
    >print "hello ";
    >not_exec_or_system("perl otherscript,pm");
    >print "world"; # World prints immediately and otherscript.pm is
    >running also


    So you want that other program and the calling script to run in
    parallel, in particular you don't want the calling script to wait until
    the other program has finished. Is this correct?

    If yes, then yes, you could use fork() and then exec() the other program
    in the child process.
    Or you could start the other program using system() and run it in the
    background using whatever method your command shell and/or OS provides
    to run programs in the background.
    Both methods are valid and will work.

    jue
     
    Jürgen Exner, Jan 14, 2009
    #6
  7. Bill H <> wrote:

    > I see



    You are not yet seeing correctly.


    > I have to
    > use system() in some manner with fork() to get what I want.



    You have to use exec() in some manner with fork() or use system()
    and put the process into the background.


    > An example using exec()



    Where's the fork() that you said you needed with exec()?


    > print "hello ";
    > exec("perl otherscript.pm");
    > print "world"; # World never prints
    >
    > An example using system()
    >
    > print "hello ";
    > system("perl otherscript.pm");



    system('perl otherscript.pm &');


    > print "world "; # World prints when otherscript.pm is done



    print "world "; # World prints immediately and otherscript.pm is running also


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Jan 14, 2009
    #7
  8. Bill H

    Guest

    Tad J McClellan <> wrote:
    > Bill H <> wrote:
    >
    > > I see

    >
    > You are not yet seeing correctly.
    >
    > > I have to
    > > use system() in some manner with fork() to get what I want.

    >
    > You have to use exec() in some manner with fork() or use system()
    > and put the process into the background.
    >
    > > An example using exec()

    >
    > Where's the fork() that you said you needed with exec()?


    It's not there. He is showing examples of what he tried that didn't work,
    which lead him to conclude he needed fork. I thought that was pretty
    obvious. More people should show us how they reached their conclusions,
    so we can point out if they are mistaken.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Jan 15, 2009
    #8
  9. Bill H

    Tim Greer Guest

    wrote:

    > Tad J McClellan <> wrote:
    >> Bill H <> wrote:
    >>
    >> > I see

    >>
    >> You are not yet seeing correctly.
    >>
    >> > I have to
    >> > use system() in some manner with fork() to get what I want.

    >>
    >> You have to use exec() in some manner with fork() or use system()
    >> and put the process into the background.
    >>
    >> > An example using exec()

    >>
    >> Where's the fork() that you said you needed with exec()?

    >
    > It's not there. He is showing examples of what he tried that didn't
    > work,
    > which lead him to conclude he needed fork. I thought that was pretty
    > obvious. More people should show us how they reached their
    > conclusions, so we can point out if they are mistaken.
    >
    > Xho
    >


    I thought it was pretty straight forward as well, but I seem to be
    having trouble wording things today myself, so I suppose people could
    have read it differently (and clearly did).
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Jan 15, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,195
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,164
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    494
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    198
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    725
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page