General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?
# 1  
Old 05-15-2009
General Q: how to run/schedule a php script from cron jobs maybe via bash from shell?

Status quo is, within a web application, which is coded completely in php (not by me, I dont know php), I have to fill out several fields, and execute it manually by clicking the "go" button in my browser, several times a day.

Thats because:
The script itself pulls data (textfiles) from a folder, and works them off one by one. As PHP has limitations regarding execution timings (server restrictions aswell), it can be, that not all the data (textfiles) are executed, so the php-script has to be restartet again and again, where it continues, where it has stopped its work.

My idea is, to run that php-script somehow, as a cron job where I have to set the maximum data to be executet, (maybe 50 textfiles), and run the script every 5 minutes, with these parameters.

What would be the wisest approach for this task? Is it generally possible?

edit:
I could find out, that I can run php from shell and also am able to give it the parameters it need, for example like this:

php functions.php -r '$_GET[parameterX]="whattever";'

Is there something like a "debug mode" which I could switch on, in order to see what parameters go to what variables or what functions are called and so on, when I call the script from the web interface.

$dbg=1; does not seem to help me in any way....

Preferebally debug information should be written into a text file.

edit 2:
So far, I am digging through the code of the php-file corrosponding to the dialog screen, and collecting & writing down the variables of the settings I need to make.

Someone please stop me, if I am on the wrong way and wasting my time.. 8-)

Last edited by lowmaster; 05-15-2009 at 06:49 PM..
# 2  
Old 05-16-2009
Well, there are two ways. One, you can invoke the php file/script using "php <filename>" . I believe you can specify command-line arguments, but to specify query parameters, just set the QUERY_STRING environment variable, ala the CGI specification.
Code:
QUERY_STRING='parm1=val1&parm2=val2' php file.php

The other thing you can do, is direct "curl" to request the page which is requested when you normally hit GO. Do a man on curl to see how you can, for instance, create a web query.
# 3  
Old 05-16-2009
Thanx alot otheus, for these golden hints.

My first try will be that curl thing... Too bad I am a total noob with php.

From what I tried, this one seems to

- log me in propperly, and
- saves a cookies.txt, as a textfile, which seems to be valid and
- loads the exact page, where the go button is, and where all the parameters have to filled into the form.

I call it with: php curl.php > test.html
and If I open the test.html with my browser, I see the exact copy of the page

But now the next step would be, clicking to the go button somehow, and of course "filling out the form" befor clicking go.

There I am stuck now... I have no idea how to do that...

<?php
$username="test";
$password="test";
$cookie="cookies.txt";
$url="http://192.168.0.234/";
$postdata = "log=". $username ."&pwd=". $password ."&subm-cia=Log%20In&redir_to=". $url ."cia/edit.php?page=CIA.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "login-cia.php");
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "login-cia.php");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
curl_close($ch);
echo $result;

exit;
?>

edit:
as far as I could research so far there are $_get and $_post ways to set variables. In my case $_get would be easy, as I could set the variables via the url. I now have to figure out how I can handle this if I have $_post is used here.

Last edited by lowmaster; 05-17-2009 at 11:51 AM..
# 4  
Old 05-18-2009
Ok, I have extended the script so now it looks like this.

As told above, logging in, and loading the correct dialog screen works well. Now I have to fill the form and send its content to the server via POST Variables.

I could research, that something like this should work, but I seem to have errors in it, as I have only the same result as above. I still get the formular loaded in the html file, but expect to see something else. What I dont understand is how does the script press the Submit "go" button? Testwise I fill the variable:searchterm=anysearchword

<?php
$username="test";
$password="test";
$cookie="cookies.txt";
$url="http://192.168.0.234/";
$postdata = "log=". $username ."&pwd=". $password ."&subm-cia=Log%20In&redir_to=". $url ."cia/edit.php?page=CIA.php";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url . "login-cia.php");
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url . "login-cia.php");
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);


function curlStart($domain, $var = false){
$fp = curl_init($domain);
curl_setopt($fp,CURLOPT_TIMEOUT,60);
curl_setopt($fp,CURLOPT_FAILONERROR,0);
curl_setopt($fp,CURLOPT_RETURNTRANSFER,1);

if($var != false) {
curl_setopt($fp,CURLOPT_POST,1);
curl_setopt ($fp, CURLOPT_POSTFIELDS, $var);
}

curl_exec($fp);

if(curl_errno($fp) != 0) {
$send = FALSE;
} else {
$send = TRUE;
}

curl_close($fp);

return $send;

}

$formURL = 'http://192.168.0.234/cia/edit.php?page=CIA.php';
$var = 'searchterm=anysearchword';

for($x=0;$x<1;$x++) {
$send = curlStart($formURL,$var);
}

exit;
?>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to run multiple cron jobs?

I have two scripts which I'm tying to run one after the other- this is what I've tried: 00 14 * * * /path/one.sh && /path/two.sh I've also tried putting each script on a different line: 00 14 * * * /path/one.sh 00 14 * * * /path/two.sh Can this be done? (1 Reply)
Discussion started by: $shell_Learner
1 Replies

2. Shell Programming and Scripting

Shell script to run multiple jobs and it's dependent jobs

I have multiple jobs and each job dependent on other job. Each Job generates a log and If job completed successfully log file end's with JOB ENDED SUCCESSFULLY message and if it failed then it will end with JOB ENDED with FAILURE. I need an help how to start. Attaching the JOB dependency... (3 Replies)
Discussion started by: santoshkumarkal
3 Replies

3. Shell Programming and Scripting

Schedule and Run By weekly shell script in cronjob

Hi All, How to schedule a shell script(script name- byweeklyreport.sh) it should run by weekly in corn job or is there any script have to write to check week and then run the above script. example-1st run March 06 2013 2nd run March 20 2013 3rd run April 3 2013... (13 Replies)
Discussion started by: krupasindhu18
13 Replies

4. Shell Programming and Scripting

Shell Script to Delete Old Cron Jobs

I have a cron entry to deploy a website portal from a staging server to a series of production servers on a weekly basis. On occasion, a random office worker who knows nothing about Linux let alone cron, will be tasked to update news picks on the staging server and then run a manual deployment... (2 Replies)
Discussion started by: AndrewT
2 Replies

5. Shell Programming and Scripting

how do I run bash script using cron job

How do I run bash script using a cron job? I have tried to just write the path of the script, but that didn't work. (1 Reply)
Discussion started by: locoroco
1 Replies

6. Shell Programming and Scripting

Shell script to schedule jobs

Dear all, I have to create a shell script which will run the job during weekday/weekend in following manner: - There are 3 jobs JB_1 JB_2 JB_3 These jobs changes its flag to "COMPLETED" in below 2 ways 1. These 3 jobs which runs after the completion of previous one i.e JB_1 runs and... (5 Replies)
Discussion started by: prajaktaraut
5 Replies

7. UNIX for Dummies Questions & Answers

shell script run by user or cron job ?

My shell script runs fine both as a cron job and when i issue it. However, I wish to differentiate when it runs as a cron-job so the "echo" statements are not issued (they get mailed to me, which i don't want). I tried checking $USER but since the cron was created in my user that does not... (5 Replies)
Discussion started by: sentinel
5 Replies

8. UNIX for Dummies Questions & Answers

Please help me with the run-away cron jobs

I started a cron job on a cluster super computer. I am just a usual user over there and has no root power. The cron job was set by using "crontab -e". It excecutes a perl command every 20 minutes. I could use crontab -l or crontab -r to list or remove the cron job. However, I just found that I... (2 Replies)
Discussion started by: dedema
2 Replies

9. Linux

cron jobs not run

hi, I am newbie, I had set cron jobs to update something on one time very day, I had tested the script, it run fine, but the cron jobs seem never run. some help? Thanks. (4 Replies)
Discussion started by: robertsnoog
4 Replies

10. UNIX for Dummies Questions & Answers

schedule many jobs using cron

HI, I need to schedule a no.of jobs using the cron facility. I currently do two kinds of scheduling,one based on the database load(after the database is loaded the program will start) and the other is based on time.....(say 10.00a.m daily) the problem is.......... When the database is loaded... (1 Reply)
Discussion started by: sireesha15
1 Replies
Login or Register to Ask a Question