Perl Background Process - Finshed Yet?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Background Process - Finshed Yet?
# 1  
Old 11-14-2007
Perl Background Process - Finshed Yet?

I have a perl process I want to run in background in a cgi, but do not
want to continue until process finished code looks like this.

sub calc(){
do calculations
return value
}

In another Perl program I call above function such as

&calc();

I want to continue after process finishes.

Any Ideas on how to do this.

Thanks
# 2  
Old 11-15-2007
for finer control over processes, check out Proc::Simple
Please note that sub calc () is a subroutine, and not a process.

If you are calling this subroutine from some other place, then the regular behavior is what you have described:
Code:
#!/usr/bin/perl
# sub_trial.pl
use strict;
sub calc ()
{
    print "in calc() now ... \n";
    sleep 10;  # wait for 10 seconds
      # or do something here
}
print "we're in main script ... \n";
calc ();
print "and continued ... \n";

# 3  
Old 11-20-2007
actually it is a little more complicated then my simple example, but
basically by the time the background process &calc finishes my page
has loaded, so I have to keep refreshing the page until &calc() finishes.

The reason for this is, once &calc enters values
into database, then final page can be reloaded only once instead of
multiple times.

Obviously, I will have to run it in a
Code:
unless(my $pid = fork()){&calc();}

or
something like that. When &calc() finishes reload page once.
# 4  
Old 11-20-2007
Does this help (I mean the non-blocking wait example shown)?

waitpid - perldoc.perl.org

Of course, as a CGI script is executed once for each HTTP request, you must keep the process ID you are monitoring somewhere, say on the filesystem or session.
# 5  
Old 12-04-2007
Its funny none of the perl books I have had anything on waitpid. I had to go to the Stevens book, but waitpid is the solution I came up with too.
.
Code:
defined(my $pid = fork()) or die "Cannot fork: $!";
if($pid){
    ## waitpid($pid, 0);
    ## or if you want the pid
    do{
        $kid = waitpid(-1, WNOHANG);
        $count_pid = $kid;
    }until $kid > 0;

    $out .= qq|<hidden type="count_pid" value="$count_pid">|;
    ## javascript redirect here
}else{
    &calc();
    CORE::exit(0);
}

It works good, but still looking for a way to improve. I may have to use AJAX or something to get rid of reloads.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

2. Shell Programming and Scripting

How to call a background process in perl?

Hi, I want to put the following code as a parallel or background process The program is as below: $n=10; #Count of files to be created. for($j=0;$j<=$n;$j++) { open(FH,">files_$j.txt") || warn "cannot create a file\n"; { print FH "count of file: $j\n"; #Sample data to be written. just... (5 Replies)
Discussion started by: vanitham
5 Replies

3. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

4. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

5. UNIX for Dummies Questions & Answers

Background Process

I need to submit a script that will continue to run after logging out and after a reboot or shutdown. I entered the following: nohup script & The script continues to run in the background after logging off the system but is killed after a reboot or shutdown. Any help would be greatly... (1 Reply)
Discussion started by: powwm
1 Replies

6. Shell Programming and Scripting

How to process and run a program in the background in perl?

Hi, I have a query about processing and running Perl program at the background. I have HTML file called Userform.html which accepts input from the user. As soon as input is given the contol goes to get.cgi (get.cgi does some processing and computing tasks). Actually get .cgi takes more... (0 Replies)
Discussion started by: vanitham
0 Replies

7. Shell Programming and Scripting

background process

i gave a copy process in the background( to copy around 100GB) , while in progress, the session got terminated. when i relogged in and checked the destination folder the copying was in progress... how could it happen(copying) when the shell terminates??? :rolleyes: (2 Replies)
Discussion started by: vinod.thayil
2 Replies

8. Shell Programming and Scripting

background process

can anybody plz tell me how can i find the background processes running. (2 Replies)
Discussion started by: Raom
2 Replies

9. UNIX for Dummies Questions & Answers

background process

How, can I hide background process's output? (5 Replies)
Discussion started by: zylwyz
5 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question