asynchronous system shell?

N

Ninja67

I have a perl script that is run on the web, that in turn, calls
another external program.

I can get the script to work, but the perl insists on waiting for the
external program to complete before completing the script.

The external program is not generating output that needs to be shown to
the user and it can take up to 2 minutes to run which is too long to
ask a web user to wait.

How do I tell perl to execute an external program and not wait for it
to complete before continuing with the perl?

I'm running on an Apache server using Unix.

Here's the code that launches the program...
system ("$sas $home/$pname.sas -log $home/$pname.log -print
$home/$pname.lst -sasuser $home -work $home >/dev/null 2>/dev/null");

I'm thinking there must be an easy modification to this call to tell
perl to keep on trucking.
 
A

A. Sinan Unur

I have a perl script that is run on the web, that in turn, calls
another external program.

I can get the script to work, but the perl insists on waiting for the
external program to complete before completing the script.
http://www.stonehenge.com/merlyn/LinuxMag/col39.html

How do I tell perl to execute an external program and not wait for it
to complete before continuing with the perl?

I'm running on an Apache server using Unix.

Here's the code that launches the program...
system ("$sas $home/$pname.sas -log $home/$pname.log -print
$home/$pname.lst -sasuser $home -work $home >/dev/null 2>/dev/null");

That's a little scary, opening a behemoth like SAS to the whole wild
world.

perldoc perlsec

Sinan
 
N

Ninja67

A. Sinan Unur said:

Wow, that's a lot of good information, but way over my head. The
author of that page is showing how to keep track of a process that was
launched, but I can't tell how he actually launches it. I'm fairly new
to perl, so I probably just don't know what to look for. In my program
though, I don't care when the externally called application finishes.
I have no desire to monitor it whatsoever. I simply want to call it
and let it go.
2>/dev/null");

That's a little scary, opening a behemoth like SAS to the whole wild
world.

perldoc perlsec

It would be, but the variables you see above have hard-coded values
assigned at the beginning of the script. Also, this is on an internal
company server not available on the internet.
 
X

xhoster

Add an "&" to the end. see:
perldoc -q background.
That's a little scary, opening a behemoth like SAS to the whole wild
world.

Not all servers are open to the whole wild world.

Xho
 
J

Jürgen Exner

Ninja67 said:
That worked! You're a genius! Now that sure was easy. I wonder why
it was so hard to find anything on that.

Because it has nothing to do with Perl (in Perl you would fork() and exec())
but with how your shell starts processes in the background.

jue
 
J

Joe Smith

Ninja67 said:
That worked! You're a genius! Now that sure was easy. I wonder why
it was so hard to find anything on that.

Hard to find?

perldoc -f system
system LIST
Does exactly the same thing as "exec LIST", except that a fork
is done first, and the parent process waits for the child pro-
cess to complete. Note that argument processing varies depend-
ing on the number of arguments. If there is more than one
argument in LIST, or if LIST is an array with more than one
value, starts the program given by the first element of the
list with arguments given by the rest of the list. If there is
only one scalar argument, the argument is checked for shell
metacharacters, and if there are any, the entire argument is
passed to the system's command shell for parsing (this is
"/bin/sh -c" on Unix platforms, but varies on other platforms).

That last line implies that you should look at the manual for the
Bourne shell to see how /bin/sh will handle the LIST. Since you
are using "2>/dev/null" is it obvious that you are at least
somewhat familiar with Bourne shell metacharacters.

-Joe
 
N

Ninja67

Joe said:
Hard to find?

perldoc -f system
system LIST
Does exactly the same thing as "exec LIST", except that a fork
is done first, and the parent process waits for the child pro-
cess to complete. Note that argument processing varies depend-
ing on the number of arguments. If there is more than one
argument in LIST, or if LIST is an array with more than one
value, starts the program given by the first element of the
list with arguments given by the rest of the list. If there is
only one scalar argument, the argument is checked for shell
metacharacters, and if there are any, the entire argument is
passed to the system's command shell for parsing (this is
"/bin/sh -c" on Unix platforms, but varies on other platforms).

That last line implies that you should look at the manual for the
Bourne shell to see how /bin/sh will handle the LIST. Since you
are using "2>/dev/null" is it obvious that you are at least
somewhat familiar with Bourne shell metacharacters.

-Joe

Thanks Joe, that's great information. You made one incorrect
assumption however. Although 99% of the perl script is written by me,
the system call was inherited from another program. I have *no idea*
what 2>/dev/null does. All I know is that without it, things weren't
working so I left it in. I probably should research what that portion
of the call actually means, but with other priorities in the way, I
took a "it's not broke so don't fix it" approach.

Thanks again.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top