in reply to executing other scripts while printing a webpage

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.
  • Comment on Re: executing other scripts while printing a webpage

Replies are listed 'Best First'.
Re: Re: executing other scripts while printing a webpage
by Scott203 (Initiate) on Jul 30, 2001 at 08:58 UTC
    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.