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

P

Public Interest

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
 
A

Anno Siegel

Public Interest said:
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
 
P

Public Interest

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?
 
G

Gunnar Hjalmarsson

Public said:
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');
 
A

Anno Siegel

Public Interest said:
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
 
P

Public Interest

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'.
 
C

Captain Dondo

Public said:
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
 
A

Anno Siegel

Public Interest said:
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
 
P

Public Interest

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?
 
D

David Efflandt

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.
 
K

Kevin Shay

(e-mail address removed) (David Efflandt) wrote in message
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top