homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
is it possible to set cronjob using PHP script
perl's set::crontab kind of feature in php
phparion




msg:3875023
 11:18 am on Mar 20, 2009 (gmt 0)

I want to use PHP to set a cronjob on server. is it possible with PHP or I need to make a shell script and then execute that using PHP?

 

mack




msg:3875045
 12:07 pm on Mar 20, 2009 (gmt 0)

To be honest a shell script triggered by php would be the most accurate way of doing this, although you can create a fake cron using php. There are a number of ways to do this, but here is one quick nasty example.

In this case we will use mysql to store the timestamp. on your php page include a file, lets say time.php within time.php we will compare the timestamp stored in the database to the current server time. If the server time is a day ahead of the time stored in the database we will carry out our daily task and update the timestamp in mysql.

This method will work simply because the site wil appear to be up to date when a user visits, but if will still be a day behind until a user hits the site.

This is indeed aa very quick and dirty way of doing it, and could be a problem for a site with high traffic simply due to the number of requests for time.php

You could make it slightlly more efficient by only including the time.php file if the current server time is within 1 hour of midnight. This is assuming you want to run a cron at midnight. for example. You could do this using a conditional if statement to compare current time without neeeding to query mysql.

Hope this gives you some ideas.

Mack.

phparion




msg:3875052
 12:21 pm on Mar 20, 2009 (gmt 0)

any geek from Zend reading this, please add this feature in next PHP version :)

thank you Mack, you are a new Mod on this forum right? :P I didn't see you before among others.

Anyway, I already knew the suggested method but that is not going to solve my problem. I am handling with a big linux based software which works like google analytics, may be better than that :), and the cronjobs are used for data filtering tasks where the number of records are above 1000,000 records. I am making a web based installation process for this software where the script is run as user NOBODY.

I was looking for a built-in function in PHP with which I could setup cronjobs. But I think even with Shell scripting it will be a pain-in-neck, as I would need root permissions to run the crontab otherwise it might create problems. So what I will do is to put directions for installer to follow for setting up cronjobs on the servers rather than handling that part from the web interface.

franco190453




msg:3875335
 5:24 pm on Mar 20, 2009 (gmt 0)

phparion:

Try this ->
<?php //Name it index.php
$socket = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10);
if($socket) {
$socketdata = “GET /cron.php HTTP 1.1\r\nHost: “.$_SERVER['HTTP_HOST'].”\r\nConnection: Close\r\n\r\n”;
fwrite($socket,$socketdata);
fclose($socket);
}
?>

<?php //Name it cron.php
sleep(10);
$time = strtotime("now");
$handle = fopen('$time.txt','w');
fclose($handle);
?>

To prove it, run index.php and close
your browser and will see cron.php
creating a text file every 10 seconds.
The only way to stop this cron is by
stopping the server (Apache).
Or if you use windows doing a taskkill.
Or forcing an exit() via a code within cron.php.
You can activate the cron remotely and
security wise is very dangerous unless
you use a hash!.

This is a cron with NO cron program and
NO cli!.
Try it, it works
(Apache, php 5.,).

It appears to be a php bug, however, to me
is indeed a wonderful feature.

Regards
Franco

phparion




msg:3875384
 6:37 pm on Mar 20, 2009 (gmt 0)

it is / could be very harmful but it is a very good technique Franco, frankly speaking :)

One other use of such technique could be in mailing list to start sending email sender script and then close browser. mostly in mailing list you cannot close the browser or disturb internet connection otherwise email sending breaks. so you can use this technique like a linux job running in the background.

very cool :)

franco190453




msg:3875712
 4:24 am on Mar 21, 2009 (gmt 0)

phparion:

Glad you liked it.
Adding to the post, the only requirement is that
php.ini has to have max_execution_time = 0

You can get your actual max_execution_time with
ini_get('max_execution_time');
And set it to 0 ->
set_time_limit(0);

any call to exit();
will stop the cron so you can safely use
return; break; etc.

Have tried it for 24 hours and have not had any problems
at all.

Regards
Franco

eeek




msg:3875721
 4:50 am on Mar 21, 2009 (gmt 0)

I want to use PHP to set a cronjob on server. is it possible with PHP

I have a couple of hundred such scripts that run from cron. Why wouldn't they?

You can either do

/usr/bin/php -1 <script>

or make the script executable and put #!/usr/bin/php -q in the first line.

phparion




msg:3875834
 11:59 am on Mar 21, 2009 (gmt 0)

@eeek: you didn't get my question at all. I know how to run scripts under cronjob, my point is how to use PHP to write crontab...

eeek




msg:3877286
 11:23 pm on Mar 23, 2009 (gmt 0)

Call "crontab -e" with the EDITOR environment variable set to a php script. That script can modify the file and when it exits crontab will re-read the file and update.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved