Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

executing other scripts while printing a webpage

by Scott203 (Initiate)
on Jul 30, 2001 at 06:31 UTC ( [id://100764]=perlquestion: print w/replies, xml ) Need Help??

Scott203 has asked for the wisdom of the Perl Monks concerning the following question:

When a user opens a page on my webserver (lets call that page "script A"), the script checks something and then has to execute another script (script B) if necessary based on the check. Right now, the user has to wait for a couple minutes for script B to execute before script A can complete and send the page to the user. I need to know if script A can initiate script B to run in the background, independent of the web page being printed. Thanks
  • Comment on executing other scripts while printing a webpage

Replies are listed 'Best First'.
Re: executing other scripts while printing a webpage
by tadman (Prior) on Jul 30, 2001 at 06:58 UTC
    The catch is that you always have to return something, even if that something is "nothing". Some sites feature a waiting page which refreshes frequently, waiting on output from something. It might feature a pleasant animated GIF, and hopefully not something along the lines of WAIT with the horrific <BLINK> tag.

    A quick way of doing this is to create script A which, when run the first time will fork off script B, and then refresh to itself with a special parameter. Script B will write to a certain filename in /tmp, for example, and script A, when called, will check for this file. When this file is there, script A prints it, possibly deletes it, and stops refreshing. The name of the file is encoded in the special parameter. If, for example, the parameter "_" was set to "fZdaPXzAQ", then you would be waiting for the file "/tmp/fZdaPXzAQ.out". The choice of name is arbitrary.

    There are a couple of things you will want to note:
    1. You might want to write a separate program to clean out any stale /tmp entries instead of leaving this up to script A. This way the user can reload the page and still get output, without you having to process all over again.
    2. Your script B should not write directly to the /tmp file, but should create a preliminary version which it writes to, then renames it at the end to the proper name. This will prevent script A from jumping the gun and printing when script B has only written half its output.
      hmm, well the thing is that script A does not need anything at all from script B to execute. Most of the time, script A wont even call script B, but when it does, it does not have to wait for script B to complete, but it does. I think I need to be more clear.. script A simply prints the webpage, and also checks to see if a certain database file is too old. If that file is old, script A calls script B (currently using server(scriptB.cgi);) which downloads a fresh database file from a remote site (this takes a couple minutes cause its a big file). Script A COULD use the old db file until script B is done, but it doesnt because it waits for the server call to complete.
        In that case, don't bother calling script B a ".cgi", since it is really a stand-alone process. CGI programs are intended to be run directly by the client, which means they operate a little differently than regular programs. For instance, using CGI.pm, they require a bit of input from STDIN to get going, unless you specify parameters on the command line.

        How about this:
        if (file_is_too_old()) { unless (fork()) { exec ("update_script"); } }
        Remember that, unless you're using an SUID script, the "update_script" will be run as your Web user, which is traditionally 'nobody'. Make sure that this user has all the required privileges to update your files, or that the SUID user does.
Re: executing other scripts while printing a webpage
by tachyon (Chancellor) on Jul 30, 2001 at 10:47 UTC

    There was a really good discussion of doing this sort of thing here Searching large files before browser timeout which includes (amongst other things) a node I wrote one way to do this and also a link to one of merlyn's columns on the same subject which covers the topic in detail.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: executing other scripts while printing a webpage
by synapse0 (Pilgrim) on Jul 30, 2001 at 09:36 UTC
    well.. if you're using system to call the script, and you're on a unix machine.. you can always make it run in the background with the shell & indicator : system("foo.pl &"); ...
    -Syn0
Re: executing other scripts while printing a webpage
by cLive ;-) (Prior) on Jul 30, 2001 at 19:09 UTC
    include script B as a sub and fork - quick and dirty...
    # print cgi response as usual, then... my $pid; if ($pid = fork) { # display run, so exit exit(0); } elsif (defined $pid) { # run script b function if $run_script_b true... if ($run_script_b) { script_B(); } } else { # raise an error coz the fork failed ... }

    Works for me. My script starts an FTP install that can take up to half an hour to run, depending on network connection.

    cLive ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://100764]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-20 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found