top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to run a php file using cronjob?

+1 vote
691 views
How to run a php file using cronjob?
posted Jan 28, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

A common method for running PHP scripts from a cron job is to use a command-line program such as curl or wget. For example, the cron job runs a command similar to the following command:

curl http://example.com/script.php

In this command, curl retrieves the web page, which then runs the PHP script.
However, there is a better way to run PHP scripts on your web site from cron jobs. You can run the script directly by using the PHP command-line interpreter. This method is just as effective, and usually faster. The following command shows how to run a script using the PHP command-line interpreter:

/usr/local/bin/php -q /home/username/public_html/script.php

In this example, the PHP command-line interpreter runs the script.php file. The -q option enables quiet mode, which prevents HTTP headers from being displayed.
Depending on the code in your PHP script, it may only run correctly when called from a specific directory. For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory. The following command shows how to call a PHP script from a specific directory:

cd /home/username/public_html/; /usr/local/bin/php -q script.php

If your script requires special configuration options, you can use a custom php.ini file. The -c option allows you to call a PHP script using a custom php.ini file:

/usr/local/bin/php -c /home/username/php.ini /home/username/public_html/script.php
answer Jan 29, 2016 by Shivaranjini
0 votes

First open a crontab i.e.
$ crontab -e

Now you will see a cronjob editor where you can add your cronjob in the following format

MIN HOUR DOM MON DOW CMD

MIN     Minute field    0 to 59
HOUR    Hour field      0 to 23
DOM     Day of Month    1-31
MON     Month field     1-12
DOW     Day Of Week     0-6
CMD     Command         Any command to be executed.

Suppose you have a PHP script myscript which you want to run every minute following would be the command -

* * * * * /usr/local/bin/php /home/mypath/myscript.php

For more examples please have a look at http://tech.queryhome.com/29093/crontab-installation-in-linux

answer Jan 29, 2016 by Salil Agrawal
Similar Questions
+1 vote

I just wanted to see the best way to securely accomplish this task. When we want to update a DB we upload to a writable directory instead of writing it directly to MySQL, I don't like having writable directories if possible.
Is there a right or better way to accomplish this?

...