How to Run PHP File Using Cron Jobs

how to run php file using cron jobs

Cron runs commands as they would be ran via the shell, so running PHP would use local paths.

You need to use a command like:

php /home/USER/public_html/cron.php

Or if including the query string is necessary, use cURL instead (if it's installed):

curl http://www.wetube.org/cron.php?id=01001

You might want to look at not exposing your cron scripts to the internet - move them to outside your web directory because if someone finds it they can constantly reload it to spam your cron scripts (i.e. sending lots of emails)

Execute PHP script in cron job

Automated Tasks: Cron

Cron is a time-based scheduling service in Linux / Unix-like computer operating systems. Cron job are used to schedule commands to be executed periodically.
You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, /etc/cron./* directories. It also checks the /var/spool/cron/ directory.

Configuring Cron Tasks

In the following example, the crontab command shown below will activate the cron tasks automatically every ten minutes:

*/10 * * * * /usr/bin/php /opt/test.php

In the above sample, the */10 * * * * represents when the task should happen. The first figure represents minutes – in this case, on every "ten" minute. The other figures represent, respectively, hour, day, month and day of the week.

* is a wildcard, meaning "every time".

Start with finding out your PHP binary by typing in command line:

whereis php

The output should be something like:

php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

Specify correctly the full path in your command.

Type the following command to enter cronjob:

crontab -e

To see what you got in crontab.

EDIT 1:

To exit from vim editor without saving just click:

Shift+:

And then type q!

How to run a PHP script using a Cron job

try

0/2 * * * * curl http://[your_id:port]/kyrill/filetest.php

Running PHP file using crontab

To execute devicecheck.php every 1 hour try the following:

Method A :: Execute the script using php from the crontab

# crontab -e
00 * * * * /usr/bin/php/var/www/devicecheck.php

Method B: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.yourwebsite.com/yourscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.yourwebsite.com/yourscript.php

UPDATE::

# chmod a+x /home/username/yourscript.php
# crontab -e
00 * * * * /home/username/yourscript.php

Setting up a Cron Job on CPanel to executes a PHP script

Since your script resides in your public_html directory you can use wget for your Cron Job

wget -O - -q https://yoursite.com/wallboard/update.php

-O - output is written to the standard output in this case it will go to the email address you specify in CPanel

-q quiet mode

Run Cron Job on PHP Script, on localhost in Windows

recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.

By the way I am sharing this for anyone that is on the same thing.

You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php)

shellscript.vbs

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing

script.bat

"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"

Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:

  1. Open Task Scheduler from windows Start menu
  2. Go to Action menu and hit Create Task...
  3. in General tab, fill the Name and Description fields as you want
  4. in Triggers tab, hit New button.
  5. from Begin the Task dropdown, select On a schedule and choose Daily
  6. from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
  7. on Actions tab, from Action dropdown, select Start a program.
  8. on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
  9. leave Add argumentts (optional) section empty.
  10. in Start in (optional) box, enter parent directory of shellscript.vbs like C:\path\to\.
  11. Hit upvote on this tutorial :) Have fun.


Related Topics



Leave a reply



Submit