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
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)
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
Executing php with crontab
Start by typing at a command line:
whereis php
Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:
5 * * * * /your/path/to/php /var/www/some/path/script.php
Edit: you may need to install the php5-cli
(Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.
Related Topics
PHP _Php_Incomplete_Class Object with My $_Session Data
Find Number Which Is Greater Than or Equal to N in an Array
Convert a String into an Array of Characters
Php.Exe' Is Not Recognized as an Internal or External Command, Operable Program or Batch File
How to Store an Array into MySQL
Check If Specific Input File Is Empty
Handling Plupload's Chunked Uploads on the Server-Side
Best Method for Sum Two Arrays
How to Solve a Timeout Error in Laravel 5
$_Files Field 'Tmp_Name' Has No Value on .Jpg File Extension
PHP Cookie Problem - Www or Without Www
Weird Error Using PHP Simple HTML Dom Parser
Setting Up Laravel on a MAC PHP Artisan Migrate Error: No Such File or Directory