CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
wait for system call (pipe)

 



narsaw
Novice

Apr 25, 2008, 2:07 PM

Post #1 of 4 (2573 views)
wait for system call (pipe) Can't Post

I have some code like this.


Code
 open (H, "command |"); 
# after command completes then move on
do_something_else();


How can I make do_something_else() wait until "command" completes to run? Command will run for many hours before completion.

Basically, do_something_else() needs the output from "command" to work properly.


(This post was edited by narsaw on Apr 25, 2008, 2:07 PM)


winfinit
User

May 8, 2008, 3:36 PM

Post #2 of 4 (2384 views)
Re: [narsaw] wait for system call (pipe) [In reply to] Can't Post

it depends on a situation but you can play with fork, example below

winfinit:~ winfinit$ cat test.pl
#!/usr/bin/perl
sleep 10;
winfinit:~ winfinit$ cat test2.pl
#!/usr/bin/perl

if($pid=fork)
{
print "inside parent\n";
waitpid($pid,0);
print "now child is done so i can continue\n";
}
else
{
print "doing some stuff in child\n";
`perl test.pl`;
}
winfinit:~ winfinit$ perl test2.pl
inside parent
doing some stuff in child
now child is done so i can continue
winfinit:~ winfinit$


hope that helps


narsaw
Novice

May 8, 2008, 3:44 PM

Post #3 of 4 (2383 views)
Re: [winfinit] wait for system call (pipe) [In reply to] Can't Post

Thanks that was helpful


meloyelo
User

May 19, 2008, 10:47 AM

Post #4 of 4 (2071 views)
Re: [narsaw] wait for system call (pipe) [In reply to] Can't Post

How about just using this:

Code
system("command > some-file"); 
do_something_else();


This will wait for command to finish before proceeding with do_something_else(). If you don't need the output from the command, just redirect it to /dev/null:


Code
system("command > /dev/null 2>/dev/null"); 
...


When you use open(H, "command |") you are creating a file handle H to read the output generated by the command as it is being generated. If you don't need the output or can wait for the command to finish, just use the system() function.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives