How Do I Use CGI->System Call As CronJob?

Discussion in 'Perl Misc' started by Public Interest, Oct 31, 2003.

  1. I got shell access, but not cronjob access. I want to use a CGI to start the
    job, and use either a html refresh or a task in windows as the cronjob
    starter.

    In fact, I can even use cgi to change the refresh rate according to
    conditions set in perl script.

    My only problem is my JOB takes too much time to run: Job A takes10 minutes,
    Job B takes 2-8 hours. The problem with CGI is that cgi has a lifetime of
    180 seconds or so and it will be broken after that time. Here is what I want
    to do:

    Part1 Start a process which is then indepentant of the CGI, so even the CGI
    is dead or finished, the new process is still running. Should I start a
    child, a sys, an exec

    Part2 Print running logs to a output file, and the output can be seen from
    web. Part2 is easy, but if I want to print the file into an absolute dir,
    should I use the absolute dir of webspace under apache or abs dir under
    shell? such as which to call dir1/dir3/html/ from dir1/dir2/cgi-bin/?
    html,
    /html,
    .../dir3/html
    /dir1/dir2/html
     
    Public Interest, Oct 31, 2003
    #1
    1. Advertising

  2. Public Interest

    Anno Siegel Guest

    Public Interest <> wrote in comp.lang.perl.misc:
    > I got shell access, but not cronjob access. I want to use a CGI to start the
    > job, and use either a html refresh or a task in windows as the cronjob
    > starter.
    >
    > In fact, I can even use cgi to change the refresh rate according to
    > conditions set in perl script.
    >
    > My only problem is my JOB takes too much time to run: Job A takes10 minutes,
    > Job B takes 2-8 hours. The problem with CGI is that cgi has a lifetime of
    > 180 seconds or so and it will be broken after that time. Here is what I want
    > to do:
    >
    > Part1 Start a process which is then indepentant of the CGI, so even the CGI
    > is dead or finished, the new process is still running. Should I start a
    > child, a sys, an exec


    If that is meant to be a question it doesn't make much sense. Starting
    a child is what you are talking about. "Starting a sys" or "an exec"
    doesn't have a recognized meaning.

    If you are asking whether you should start the child via exec() or via
    system() (not to mention other alternatives), that is a secondary point.
    Depending on details of the background job and its communication needs,
    one or the other method may be more practical.

    > Part2 Print running logs to a output file, and the output can be seen from
    > web. Part2 is easy, but if I want to print the file into an absolute dir,


    There is no such thing as an "absolute directory". There are directories
    (and files) which you can *access* through an absolute or relative
    path name.

    > should I use the absolute dir of webspace under apache or abs dir under
    > shell?


    What is the difference? Is the web server running in a chroot environment?
    Otherwise, an absolute path is an absolute path, for a shell just like
    for apache.

    > such as which to call dir1/dir3/html/ from dir1/dir2/cgi-bin/?
    > html,
    > /html,
    > ../dir3/html
    > /dir1/dir2/html


    Since you (reasonably) want to use an absolute path, the first and third
    alternatives are out -- they are relative paths. Why are you mentioning
    them? The other two are absolute paths, but only one will address the
    directory you want. Use the one that addresses the directory you want.
    (The last one looks most plausible, but there is no way of telling.)

    Anno
     
    Anno Siegel, Oct 31, 2003
    #2
    1. Advertising

  3. I guess I forgot to mention that in part1:

    I think I should use exec(), but I need to CD it first. But exec will leave
    the CGI program, so how do I exec 2 things?

    For example, I want to run
    cd /home/mydir
    sh something.sh

    if I put
    exec ('cd /home/mydir');
    exec ('sh something.sh');

    the first exec will have nothing to do with the second one. so should I use
    system at the first then exec at the second one?
     
    Public Interest, Oct 31, 2003
    #3
  4. Public Interest wrote:
    > For example, I want to run
    > cd /home/mydir
    > sh something.sh
    >
    > if I put
    > exec ('cd /home/mydir');
    > exec ('sh something.sh');
    >
    > the first exec will have nothing to do with the second one.


    Try

    exec ('cd /home/mydir; sh something.sh');

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 31, 2003
    #4
  5. Public Interest

    Anno Siegel Guest

    Public Interest <> wrote in comp.lang.perl.misc:
    > I guess I forgot to mention that in part1:
    >
    > I think I should use exec(), but I need to CD it first.


    Why do you think you still need to change the directory? I thought
    you were going to use absolute path names.

    > But exec will leave
    > the CGI program, so how do I exec 2 things?


    You can only exec one thing. See "perldoc -f exec".

    > For example, I want to run
    > cd /home/mydir
    > sh something.sh
    >
    > if I put
    > exec ('cd /home/mydir');
    > exec ('sh something.sh');
    >
    > the first exec will have nothing to do with the second one. so should I use
    > system at the first then exec at the second one?


    Wouldn't help. "System" runs the "cd" in a forked process. See "perldoc
    -f system". You really should have taken a look there before.

    You can change the directory in the mother process before you exec. The
    resulting process will inherit the working directory.

    Anno
     
    Anno Siegel, Oct 31, 2003
    #5
  6. Another very very big problem:

    The CGI still want for the exec to be finished!!!. I have printout from the
    EXEC. and it kept showing on my browser!!!

    So they are still not independent.

    And only the Child's print is out and parent's output is not printed..

    Try this: in test.cgi

    #!/usr/bin/perl

    print "Content-type: text/html\n\n";
    exec ('sh test.sh);
    print 'ok this is parent';

    in test.sh:
    ls -al


    Then in my browser, it shows all the files, but no 'ok this is hte parent'.
     
    Public Interest, Oct 31, 2003
    #6
  7. Public Interest wrote:
    > The CGI still want for the exec to be finished!!!. I have printout from the
    > EXEC. and it kept showing on my browser!!!


    man bash:

    exec If command is specified, it replaces the shell.

    When you run 'exec', there is no longer a script to return to. exec is
    the last command any script will run.

    If you want to return to it, use 'system'.

    I'm guessing that perl and sh are consistent in the use of 'exec' and
    'system'.

    -Dondo

    --
    What am I on?
    I'm on my bike, o__
    6 hours a day, busting my ass. ,>/'_
    What are you on? --Lance Armstrong (_)\(_)
     
    Captain Dondo, Oct 31, 2003
    #7
  8. Public Interest

    Anno Siegel Guest

    Public Interest <> wrote in comp.lang.perl.misc:
    > Another very very big problem:
    >
    > The CGI still want for the exec to be finished!!!. I have printout from the
    > EXEC. and it kept showing on my browser!!!
    >
    > So they are still not independent.


    That is because the child's STDOUT is the same as the parent's. If you
    don't want to see output, re-direct it in the child process.

    > And only the Child's print is out and parent's output is not printed..
    >
    > Try this: in test.cgi
    >
    > #!/usr/bin/perl
    >
    > print "Content-type: text/html\n\n";
    > exec ('sh test.sh);
    > print 'ok this is parent';
    >
    > in test.sh:
    > ls -al
    >
    >
    > Then in my browser, it shows all the files, but no 'ok this is hte parent'.


    Of course it doesn't. Would you *please* take a look at the documentation
    of the functions you're using? A successful call to exec() does *not*
    return to the calling process. If you want that, use system().

    Anno
     
    Anno Siegel, Oct 31, 2003
    #8
  9. Because I want the Child to be seperated and totally independent from
    Parent, so system does not work for me. I want both child and parant to live
    and run until finish. I want something like & in shell

    ls &

    How do I have a & in perl?


    > Public Interest wrote:
    > > The CGI still want for the exec to be finished!!!. I have printout from

    the
    > > EXEC. and it kept showing on my browser!!!

    >
    > man bash:
    >
    > exec If command is specified, it replaces the shell.
    >
    > When you run 'exec', there is no longer a script to return to. exec is
    > the last command any script will run.
    >
    > If you want to return to it, use 'system'.
    >
    > I'm guessing that perl and sh are consistent in the use of 'exec' and
    > 'system'.
    >
    > -Dondo
    >
    > --
    > What am I on?
    > I'm on my bike, o__
    > 6 hours a day, busting my ass. ,>/'_
    > What are you on? --Lance Armstrong (_)\(_)
    >
     
    Public Interest, Oct 31, 2003
    #9
  10. On 31 Oct 2003, Anno Siegel <-berlin.de> wrote:
    > Public Interest <> wrote in comp.lang.perl.misc:
    >> Another very very big problem:
    >>
    >> The CGI still want for the exec to be finished!!!. I have printout from the
    >> EXEC. and it kept showing on my browser!!!
    >>
    >> So they are still not independent.

    >
    > That is because the child's STDOUT is the same as the parent's. If you
    > don't want to see output, re-direct it in the child process.


    In other words look for daemonize in 'perldoc perlipc'. It shows how to
    fork a child and disassociate itself from the parent (your CGI). Of
    course your CGI should provide some proper output to either just
    acknowledge, or advise the browser if the fork fails.

    Once you fork the child and disassociate it from the parent, it could run
    your 2 other processes.

    --
    David Efflandt - All spam ignored http://www.de-srv.com/
    http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
    http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
     
    David Efflandt, Oct 31, 2003
    #10
  11. Public Interest

    Kevin Shay Guest

    (David Efflandt) wrote in message
    news:<>...
    > In other words look for daemonize in 'perldoc perlipc'. It shows how to
    > fork a child and disassociate itself from the parent (your CGI). Of
    > course your CGI should provide some proper output to either just
    > acknowledge, or advise the browser if the fork fails.


    Here's a good article by Randal that demonstrates how to do this in a
    CGI context. It describes a method by which a CGI script can fork a
    process, then "watch" the running child process and display an updated
    status message in the browser without timing out:

    http://www.stonehenge.com/merlyn/LinuxMag/col39.html

    Kevin
    --
    perl -MLWP::UserAgent -e '$u=new LWP::UserAgent;$u->agent("japh");
    print join(" ",(split(/\s+/,(split/\n/,$u->request(HTTP::Request->
    new(GET=>join("",split(/\n/,"http://groups.google.com/groups?selm=
    4365%40omepd.UUCP&output=gplain"))))->content)[60]))[0..3]),",\n"'
     
    Kevin Shay, Oct 31, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shailan
    Replies:
    2
    Views:
    1,000
    Shailan
    Dec 15, 2003
  2. Replies:
    4
    Views:
    125
    Pierre-Charles David
    Nov 22, 2006
  3. Matthew Lagace

    Problem with cronjob and ruby script

    Matthew Lagace, Jun 5, 2009, in forum: Ruby
    Replies:
    19
    Views:
    313
    Hassan Schroeder
    Jun 5, 2009
  4. Guy
    Replies:
    4
    Views:
    820
    Malcolm Dew-Jones
    Dec 11, 2003
  5. ReggieC
    Replies:
    11
    Views:
    293
    Tim Greer
    Oct 14, 2008
Loading...

Share This Page