Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using exec()

Reply
Thread Tools

Using exec()

 
 
Bill H
Guest
Posts: n/a
 
      01-14-2009
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
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      01-14-2009
Bill H <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-14-2009
Bill H <(E-Mail Removed)> 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
 
Reply With Quote
 
Bill H
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 12:12*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> Bill H <(E-Mail Removed)> 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
 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      01-14-2009
In article <(E-Mail Removed)>,
Bill H <(E-Mail Removed)> 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 <(E-Mail Removed)> 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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-14-2009
Bill H <(E-Mail Removed)> 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
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-14-2009
Bill H <(E-Mail Removed)> 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/"
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-15-2009
Tad J McClellan <(E-Mail Removed)> wrote:
> Bill H <(E-Mail Removed)> 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.
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      01-15-2009
(E-Mail Removed) wrote:

> Tad J McClellan <(E-Mail Removed)> wrote:
>> Bill H <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a Link Button to redirect to another page by using data from =?Utf-8?B?R1REcml2ZXI=?= ASP .Net 1 02-16-2005 07:04 PM
Error page using the Application_Error void dosnt work when using DIV for a please wait message s_erez@hotmail.com ASP .Net 2 12-24-2004 12:11 PM
no code in webform using vs.net, but in webform using notepad timmso ASP .Net 1 12-12-2003 04:30 PM
Using GetOleDbSchemaTable to get SQL Server Field Description - using pete ASP .Net 1 08-29-2003 10:50 AM
Re: MVP? Index error on nested element using System.xml but NOT using msxml??? William F. Robertson, Jr. ASP .Net 1 06-25-2003 08:08 PM



Advertisments