hd.
hd.

Reputation: 18306

run php script automatically

i have a php script that should be run automatically every day. as the php script is run on a request,how can i do it? is there any way else using cronjob task?

Upvotes: 7

Views: 10398

Answers (5)

Catalin
Catalin

Reputation: 868

If cron is not available you could execute a php script in CLI which will run all the time.

In the script you can make a infinite while loop.

In the while loop, check a file on disk or a db record (you can controll this file or db record from an external script, telling the looping script what to do (CLI execute another script at a given hour) and when to exit)

If you use a database,don't forget to initialize and close the db connection each time the loop runs.

I'd sleep the loop every 1 min or so.. you could use this instead of linux cron for many more things.

Upvotes: 0

Alfred
Alfred

Reputation: 61773

If cron isn't available in some sort of way you could use Google app engine's cron for this. Because cron is the way to go.

Upvotes: 1

pooja
pooja

Reputation: 2422

use cron job option who start automatically and give result before 24 hours

Upvotes: 4

Silver Light
Silver Light

Reputation: 45922

Two options:

  1. Use crontab demon
  2. Hire a worker and make him open script in a browser every 24 hours

The choice is yours :)

To use crontab, type crontab -e in console, the text file opens. Add a line at the end:

0 0 * * * /usr/bin/php /var/www/mysite/httpdocs/daily_stats.php

Where:

0 0 * * * - run every day at 00:00

/usr/bin/php -path to your PHP (can be determined by which php command)

/var/www/mysite/httpdocs/daily_stats.php - path to your PHP script

if which php outputs nothing, install PHP cli by running:

sudo aptitude install php5-cli 

Good luck!

Upvotes: 10

Dani
Dani

Reputation: 15069

Use the cron job, this is the best solution. otherwise, you can run an infinite loop inside php and sleep 24 hours. horrible solution though.

Upvotes: 1

Related Questions