Solved

Start an asyncronous script from a cgi page?

Posted on 2002-03-25
10
166 Views
Last Modified: 2010-03-05
I need to start a script that runs for a few hours using a cgi page. I can simply run the script from the browser, but the user will have to wait these few hours (and will most probably time out) and I don't want this. I want to be able to just run the script independantly of the browser...

Is there a way to do this? Say, can I schedule a cronjob with a cgi page? Or is there another solution?
0
Comment
Question by:polygon
  • 6
  • 2
  • 2
10 Comments
 
LVL 1

Expert Comment

by:mehdi
ID: 6894538
Just take the nuts and bolts of what you need to do out of your "cgi-page" and put it into a regular perl file.  Set a cronjob to run that.  This is a very common systems administration task.

There is no reason why a cronjob would not work on your actual script.  Just beware that the execution of your code is not dependant on things such as:

* URL - your not passing parameters to the script with a GET string
* SERVER_NAME, and other environment variables which are quite often used from CGI scripts.

Hope that helps

Mehdi
0
 
LVL 2

Author Comment

by:polygon
ID: 6894594
The cronjob thing works fine for me... But how do I set up a cronjob from within a cgi page (this may be a trivial question, but I am not much into Perl now :))).

I need to be able to set the cronjob to run at the moment (or maybe a few seconds after) the user clicks "Start the job" link...
0
 
LVL 2

Author Comment

by:polygon
ID: 6894627
The cronjob thing works fine for me... But how do I set up a cronjob from within a cgi page (this may be a trivial question, but I am not much into Perl now :))).

I need to be able to set the cronjob to run at the moment (or maybe a few seconds after) the user clicks "Start the job" link...
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6895242
polygon,

which OS your CGI runs on?
0
 
LVL 1

Expert Comment

by:mehdi
ID: 6895940
Let me get this strait.. you want to put a scheduled task onto the system using perl ?

Well, your obviously using a unix based system the files you need to edit are in /var/spool/cron - something like that.

You can definately edit this file with perl - should not be a problem as long as your permissions are set up correctly - however this is not generally a good idea and i would advise against it.

Cheers

Mehdi

Mehdi
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 5

Accepted Solution

by:
Sapa earned 300 total points
ID: 6896098
> I need to start a script that runs for a few hours using a cgi page.

What the reason to use cron if you just want to run background process, not to postpone the launch until specified time?

On unix-like systems the only you need is fork another
proces from your CGI, and detach it. For example:


use CGI;
my $query = new CGI;
...

# Create unique filename for backgroung results
my $result_html = sprintf("%i%i.html", time, $$);

unless(defined(my $kid = fork)) {
    print $query->header;
    print "<H2>Cannot fork: $!</H2>";
}

if ($kid) {
    # Parent process
    print $query->header;
    print "<H2>Your request is processing now</H2><BR>";
    print "Please visit <A HREF=$result_html>";
    print "this link</A> 4 hours later";
    exit;
}

# child process

open STDIN, '< /dev/null';
open STDOUT, "> $result_html";
open STDERR, '> bgerror.log';

# execute your long code here

0
 
LVL 1

Expert Comment

by:mehdi
ID: 6896131
Hmm, i see what you are saying now..

im thinking - change the system time using perl - forcing your cronjob to gogo.. then set it back - your other process is underway and bobs your uncle..

That is a real HACK, but should work i think.

Im sure there is a better way to do it.. but cant think of it right now..

Mehdi
0
 
LVL 1

Expert Comment

by:mehdi
ID: 6896162
Hmm, i see what you are saying now..

im thinking - change the system time using perl - forcing your cronjob to gogo.. then set it back - your other process is underway and bobs your uncle..

That is a real HACK, but should work i think.

Im sure there is a better way to do it.. but cant think of it right now..

Mehdi
0
 
LVL 1

Expert Comment

by:mehdi
ID: 6896177
polygon,

Ive had a thought.

Why dont you change the way your program works altogether.

You are obviously carrying out a lengthy procedure based on user interaction through cgi.Your already asking the user to come back later (when the process completes) so why not do it this way,

* Get the user input and store it as "un-processed entries", tell the user to come back later, say an hour.
* set up a crongjob to run on the server every 30 minutes or so to process the unprocessed entries.
* When the cronjob is run, it processes "un-processed" entries and marks them as "processed".
* When the user returns they are shown the result of the processed entry (if its processed).  If the entry is not yet processed, they are told to come back later.

Hope that helps, though its not so much a solution - more a work around, but i think it achieves the same thing.

Mehdi
0
 
LVL 1

Expert Comment

by:mehdi
ID: 6896318
Poly,

Have you tried executing $test = `command.pl &`; ? note the backticks - execute a system command... the "&" should make it run in the background.

M

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now