Page 1 of 1
Use cron job to run php script
Posted: Wed Nov 09, 2005 7:49 am
by tlholley
Hi, I don't know much about PHP. I have been using a cron job to run a script with no problems. However, I can only set the cron job to run at the same time each day and I want to add a more random appearance to the running of the script. What I want is to set the cron job to run a PHP script that will create a random refresh time and then redirect to the original script that I have been using.
I combined a couple of scripts I saw and came up with this:
Code: Select all
<?php
srand(time());
$random = (rand()%5);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="refresh" content="<?=$random;?>;URL=http://www.mysite.com/rssblog/post.php">
</head>
<body bgcolor="#FFFFFF" text="#000000">
</body>
</html>
If I type in the url to this random.php script it works fine, but when I set the cron job to run the random.php, it doesn't seem to work.
I don't need the post.php page to actually load, just to call the page and disconnect.
Is this possible?
Posted: Wed Nov 09, 2005 8:22 am
by foobar
I usually don't do cronjobs, but instead I include an "auto.php" file in every script. This included script checks if the current time is greater than the last "cronjob" time by x seconds; the last "cronjob" time is saved in a file on the server, and the time is a normal PHP timestamp.
It's easy and it works. You could have multiple times in your cronjob-time file, and execute code accordingly.
auto.php?
Posted: Wed Nov 09, 2005 8:34 am
by tlholley
I'm sorry, like I said, I don't know PHP.
Where can I get the code for this auto.php and exacty what do I do to make it work?
Would the auto.php be a seperate file or included in the post.php file? When the cron job runs, say 3am, it would then delay executing post.php for a random time up to 30 minutes? That is what I am wanting.
Re: auto.php?
Posted: Wed Nov 09, 2005 8:51 am
by foobar
tlholley wrote:I'm sorry, like I said, I don't know PHP.
PHP Manual.
tlholley wrote:
Where can I get the code for this auto.php and exacty what do I do to make it work?
See above.
tlholley wrote:
Would the auto.php be a seperate file or included in the post.php file? When the cron job runs, say 3am, it would then delay executing post.php for a random time up to 30 minutes? That is what I am wanting.
Well, you can of course have the code in your post.php directly if you want to. And what exactly the cronjob script does is up to you. No limitations whatsoever.
PHP manual
Posted: Wed Nov 09, 2005 8:57 am
by tlholley
foobar, I appreciate you trying to help but I'm afraid your replies are a little too vague for me. I went to the manual and searched for auto.php and it found nothing. Where in this manual can I find out about auto.php? Thanks.
Re: PHP manual
Posted: Wed Nov 09, 2005 9:19 am
by foobar
tlholley wrote:foobar, I appreciate you trying to help but I'm afraid your replies are a little too vague for me. I went to the manual and searched for auto.php and it found nothing. Where in this manual can I find out about auto.php? Thanks.
The name auto.php is just a name I came up with. It's a file that I created. And it happens to be doing the cronjob stuff. I'm at uni now, but when I get home, I'll give you a sample of what the code looks like.
Regarding my replies being vague, I'm afraid it's more to do with misunderstanding than accuracy on my part. Sorry to be confusing you, but since you already started out talking about cronjobs, I assumed you had some experience with web-programming. It seems that you don't, so I would still encourage you to just ake it easy, start slowly. Go to the PHP Manual and do the first few tutorials. Just to get a grasp of what people are talking about. That way you won't be as overwhelmed when people start throwing techy stuff at you.
Cheers,
Tom
Posted: Wed Nov 09, 2005 2:51 pm
by foobar
Here's what it looks like, without the stuff that wouldn't interest you anyway:
The page you want it to be called from:
Code: Select all
<?php
require_once('includes/auth.php');
//...some other stuff
?>
*Explanation of including:
include() Simply include a php file and treat the code as the code of the actual page. Quits with a warning upon failure.
include_once() Same as above, but subsequent inclusion of the same file will be ignored.
require() Same as include(), but terminates the script on failure.
require_once() Same as above, but subsequent inclusion of the same file will be ignored.
My auto.php file (from a community website I made):
Code: Select all
<?php
//...some stuff
/*---Do cron job--*/
$cjtime = (int) implode(file('data/cronjob.txt')); //Open the file (file()) and implode() it to remove carriage-returns.
if (time() - $cjtime > 300) { //Run the following every 300 seconds
//NOTE: YOU CAN IGNORE EVERYTHING BELOW THIS LINE.
//I LEFT IT THERE FOR DEMONSTRATIVE PURPOSES!
$rescount = 0;
$vuid = array();
$iuid = array();
$quid = array();
$vsize = array();
$isize = array();
/* Count contrib.s */
$sql = 'SELECT userid, size FROM '.$conf['tbl_images'];
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$iuid[] = $row['userid'];
$isize[$row['userid']] += $row['size'];
$rescount++;
}
$sql = 'SELECT userid, size FROM '.$conf['tbl_videos'];
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$vuid[] = $row['userid'];
$vsize[$row['userid']] += $row['size'];
$rescount++;
}
$sql = 'SELECT userid FROM '.$conf['tbl_quotes'];
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$quid[] = $row['userid'];
$rescount++;
}
$fp = fopen('data/rescount.txt', 'w');
fwrite($fp, $rescount);
fclose($fp);
$iuid = array_count_values($iuid);
$vuid = array_count_values($vuid);
$quid = array_count_values($quid);
$sql = 'SELECT id FROM '.$conf['tbl_users'];
$rs = mysql_query($sql);
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
$uid = $row['id'];
$ucount[$uid] = $iuid[$uid] + $vuid[$uid] + $quid[$uid];
$uquota[$uid] = $isize[$uid] + $vsize[$uid];
}
foreach ($ucount as $u => $c) {
$sql = 'UPDATE '.$conf['tbl_users'].' SET contributions = '.$c.' WHERE id = '.$u;
mysql_unbuffered_query($sql);
}
foreach ($uquota as $u => $q) {
$sql = 'UPDATE '.$conf['tbl_users'].' SET quota = '.$q.' WHERE id = '.$u;
mysql_unbuffered_query($sql);
}
/* Generate new cron time */
$fp = fopen('data/cronjob.txt', 'w');
fwrite($fp, time());
fclose($fp);
}
?>
Posted: Wed Nov 09, 2005 2:56 pm
by Luke
Doesn't this rely on somebody visiting the page on a daily basis? This won't execute without somebody actually going to the page this is included in right?
Posted: Wed Nov 09, 2005 3:09 pm
by foobar
One armed space goat wrote:Doesn't this rely on somebody visiting the page on a daily basis? This won't execute without somebody actually going to the page this is included in right?
Yup. Usually, that isn't a problem, since cronjobs usually don't change anything that has nothing to do with users. IF you want to do a DB dump / backup at exactly 3am, then this isn't the right solution for you. However, if you want one to happen approximately every x hours/days/weeks, then it's just fine. Should work for most purposes. Plus, tlholley wanted to "add more random appearance to the running of the script", which this demonstrates very well!

Posted: Wed Nov 09, 2005 3:17 pm
by Luke
Yea I guess it really doesn't matter anyway as long as the job executes before anything else on the page.
Posted: Wed Nov 09, 2005 3:22 pm
by foobar
One armed space goat wrote:Yea I guess it really doesn't matter anyway as long as the job executes before anything else on the page.
That's usually the case. However, if you want to do a DB backup while people are surfing, you'll have gigantic loading times. For that, you can however use a cronjob service, such as
http://www.cronjob.de/ (German).
Posted: Wed Nov 09, 2005 3:26 pm
by Luke
I'd be pretty <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> if I had to wait for a database backup before entering devnet... haha
Posted: Wed Nov 09, 2005 4:35 pm
by tlholley
Perhaps I should clarify. I don't understand everything about this auto.php thing, but I'm not convinced that it is what I am looking for.
I have a php script that allows me to post to a blog automatically as per the cron job. I usually let the cron job run once a day at 3am. The cron job runs post.php and it posts a new blog.
What I am wanting to accomplish is to create a less automated look to the times that the blog posts.
Instead of the cron job calling the post.php script, I would like it to call a random.php script that would generate a random amount of time, between 1 and 30 minutes would be good, and then refresh/redirect to the post.php script.
The script I came up with works fine when I enter the random.php URL directly into my browser, but it is not working when the cron job calls the random.php script. I just want to know what I should do to get this to work.
Thanks.
Posted: Wed Nov 09, 2005 6:12 pm
by Luke
What he's talking about would be perfect then... because it's not going to post the script at the same time, but you have to count on somebody coming to your site within that 30 minutes. (even if it's you... but that would kinda take away from the whole automation thing)