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

Hello Monks,

I am writing a cgi perl program and my requirement is to
fire a background process and proceed with the rest of the code.

I am using the 'system' with '&' to do this.
And this background program expects a parameter 'uid' (for userid).

Inside this program I use 'CGI' module to read the parameters.
I used the following code to fire this process -

system "/home/blahblah/www/cgi-bin/indexer ?uid=user1 &";

But this program is not able get the uid parameter. I am not an expert in Perl programming. I looked at the perl and cgi books and did this experiment. Is there any way to achieve the backgroud execution while passing the parameters?

Thanks!
  • Comment on Passing arguments to a background process...

Replies are listed 'Best First'.
Re: Passing arguments to a background process...
by Roger (Parson) on Dec 05, 2003 at 04:41 UTC
    I am somehow puzzled why you want to use CGI to do command-line processing. Nevertheless CGI scripts can run as normal programs. You will need to drop the question mark before uid. Otherwise user1 is treated as the value of the "?uid" variable.
    use strict; use CGI; my $q = new CGI; my $q_uid = $q->param('uid'); print "$q_uid\n";
    You call the script with -
    script.cgi uid=user1
Re: Passing arguments to a background process...
by holo (Monk) on Dec 05, 2003 at 08:07 UTC

    Take a look at the -s switch in perlrun or use the more sane Getopt::Long approach. Don't use a drill to pick your nose. (for obvious reasons)

Re: Passing arguments to a background process...
by l3nz (Friar) on Dec 05, 2003 at 09:45 UTC
    I'd drop the question mark from the command line to solve your problem, but then, why should you use a CGI interface for the program you're launching in the background?

    Apart for this, I' don't like much the idea of launching another process from a web page. You sure there is no smarter way to do it, like having a process running and queuing requests to it? As you don't know when the process will end, I presume what you need to do is not synchronous. You could just add a flag on a table (or write a flag-file in a directory) and then a single process would look for existing flags and process them one at a time.

    This has the adventages that you 'pay' for program startup only once (process creation and code compilation, if it's in Perl), you have no racing conditions to take care of, and you can write readable logs (as it will process one thing at a time), and as you have only one instance running there is only one copy of the program in RAM at a given time.

    My 0.02 euros

      i think you need to learn the difference between a cgi program, which is a program that processes and/or generates web pages, and a normal (non-cgi) program. A CGI program takes its' parameters from HTML forms and receives them via param{'var_name'} call. This is the CGI interface. When you are calling your background process directly from another program, the interface is standard (not redirected) input/output and @ARGV to get at arguments passed to it. Mind you, you have to be very careful in the design of both the CGI program and your background process, as the web users will potentially create many instances of those programs in memory simultaneously. Therefore, if your programs deal with any files, they must be written as not to interfere with each other or corrupt files. I recommend that you borrow/buy a book on perl CGI to learn a great deal more....CGI programs, i.e. perl programs that "use CGI;" have their standard input,output,and error handles re-directed. it's like switching from programming for a console app. to a windows based app., input/output and standard arguments are handled differently.
        Thanks very much for your replies. 
        
        I will look into the options in more detail and work on it.
        
        I like the option of using a file or table and let only 
        one instance of the program running. 
        
        I might have said it wrong when I said that I want to run 
        a CGI program in batch. It is just a perl program. 
        
        I tried using the @ARGV and STDIN, but I was not 
        successful in transfering the data. I was able to pass the 
        file name, but not a variable with data or data itself. 
        Can you give a sample code or how to use these to pass 
        parameters (data or variable) to other program than the 
        file names.  
        
        Thanks very much for you help!