Forum Moderators: coopster

Message Too Old, No Replies

Schedule task in PHP

         

ph_eze_nouz

10:44 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



Hi,

Is it possible to schedule some tasks in PHP to run automatically on the server at a certain time of the day?

Thx - pH

jatar_k

10:48 pm on Aug 5, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



we use crontab for this

[en.wikipedia.org...]

dkin

11:09 pm on Aug 5, 2004 (gmt 0)

10+ Year Member



I am having many problems with my crontab, I set it to collect all happenings in the day, it runs at 11:50 pm but it seems to be late, say 12:01 then I get empty emails.

jollymcfats

12:33 am on Aug 6, 2004 (gmt 0)

10+ Year Member



I use a cron job as well.

You can run the tasks in the webserver via cron too. Sometimes this makes sense if you've got a complicated auto_prepend_file setup or shared memory stuff going on.

I do this by using wget in my crontab:


*/30 * * * * wget http://localhost/cron/someJob.php >/dev/null 2>&1

You'd probably want to restrict your directory of cron scripts to only allow access from localhost...

TheBlueEyz

1:15 am on Aug 6, 2004 (gmt 0)

10+ Year Member



Your other option, if your cron jobs aren't operating properly, is to set up the PHP script to run everytime a visitor hits your pages.

Then, when the script runs, it tests to see how long it was since the last time it ran. If it has been long enough, go ahead and run again. If not, just don't do anything.

This is the "poor man's cron job" and is very popular for people who don't have access to the cron tab.

If you can't guarantee that you'll get enough traffic to fulfill the scheduling needs, you can set up a very simple meta tag in a php file that runs every 5 minutes, or however often you want it to run.

You can do that by doing this:

In example.php:

<html>
<head>
<title>Poor Man's Cron</title>
<meta name="refresh" content="5; [example.com...]
</head>

<?php
//do your cronjob processing here
?>

<body>Cron job done!</body>

</html>

That'll refresh every 5 seconds to that URL. 5 minutes would be 300, etc.

So just save example.php on your server, open it in your browser, and then minimize it. As long as that browser is open it'll refresh on schedule.

ph_eze_nouz

9:12 am on Aug 6, 2004 (gmt 0)

10+ Year Member



Hi

Many thanks for your posts ;) How do you do to connect the server and use the cron table? Is it throught a Telnet?

I am looking forward to practicing this, also do you have any online tutorials?

Cheers

pH

ph_eze_nouz

4:05 pm on Aug 9, 2004 (gmt 0)

10+ Year Member



anyone? ;)

jatar_k

4:08 pm on Aug 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



usually through telnet or ssh but it is really host specific. I would contact your host and see if it is even available to you and how you would access it.

TheBlueEyz

10:29 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Sorry for the delay -

I always just use Cpanel but you can do it through SSH also.

Lookup a program called "Putty". Note you won't be able to use it if your host has shell access disabled.

I would contact your host and ask if they make the cron tab available, and if not, if they would be able to install it.

Most hosts won't let you use the shell because it's a security risk.

ph_eze_nouz

11:04 am on Aug 10, 2004 (gmt 0)

10+ Year Member



Many thanks Jatar_k and TheBlueEyz for all this information ;) I will contact my host and investigate.

Again, thank a lot for your help