Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

is it possible to set cronjob using PHP script

perl's set::crontab kind of feature in php

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

WebmasterWorld Senior Member 5+ Year Member

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?
12:07 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Administrator mack is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

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.


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

WebmasterWorld Senior Member 5+ Year Member

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.

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

5+ Year Member


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”;

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

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.


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

WebmasterWorld Senior Member 5+ Year Member

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 :)

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

5+ Year Member


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
And set it to 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.


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

WebmasterWorld Senior Member 10+ Year Member

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.

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

WebmasterWorld Senior Member 5+ Year Member

@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...
11:23 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

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.

Featured Threads

Hot Threads This Week

Hot Threads This Month