How to write a Cron Job to execute simple php script? - php

I have a simply php script on my server and want it to be run every 2 minutes using a cron job.
*/2 * * * * http://mydomain.com/_adder.php
I suspect the command syntax is wrong.
Do I need to add a command before the script url? Another way to run the script?
Any help is very much appreciated.

the cron-job will simply execute a program on the (local) machine.
a URL is NOT a program. it's a link to a ressource.
whether this ressource triggers a PHP-script execution is not of cron's business.
in any case, you could run a cron-job that will periodically visit a given URL. e.g. using the wget command (a "non-interactive web-page downloader")
*/2 * * * * wget --quiet -O /dev/null http://mydomain.com/_adder.php

you can do it as umläute suggest, but being a local file it's actually faster to access from command line php like this:
*/2 * * * * php /path/to/file/_adder.php
there are differences running a script from the command line vs via a browser that may effect the script.
you may need the full path to php on some systems

use php-cgi to make GET request similar to a web browser .
/usr/bin/php-cgi /your_path/_adder.php
if you're using Linux you can use which php-cgi to locate for php-cgi or search for php7.2-cgi if php 7.2 installed on your machine or search using different version . for Windows users search for php-cgi.exe .

Related

Cron job keeps failing in cPanel

My cron job keeps failing in cPanel:
/bin/sh: /opt/cpanel/ea-php73/root/etc: is a directory
I have changed the code, previously it was /usr/local/bin/php
This is the full code:
0 0,12 * * * /opt/cpanel/ea-php73/root/etc /home4/***/****.org/api/fetch.php
Does anyone know the reason? The file fetch.php works fine otherwise. How do I know where the PHP command is?
If you want to run a PHP file in a cron job, you should run with the PHP interpreter. But your error shows that the cron job is running by the Bash interpreter.
This means you have issue with this part /opt/cpanel/ea-php73
You should either revert your interpreter back to /usr/local/bin/php
Or you can use the curl command to run the webpage from an external source.
Eg: 0 0,12 * * * curl ****.org/api/fetch.php
In the PHP script, you're directly executing an incorrect shell command. Probably a missing / prefix in a path.

Run Crontab in Linux when a new file created in a directory

I have a set a crontab in my ubuntu 16.04 to fetch data from /var/www/html/import/IMPORT-DATA.CSV through a PHP script.
*/5 * * * * php /var/www/html/cron/import-csv.php
Its working fine, and after fetching the data my PHP script will delete the file (/var/www/html/import/IMPORT-DATA.CSV).
I want to set up a script in Linux (either a crontab or something else) which can run my PHP script once if the IMPORT-DATA.CSV file uploaded in the directory /var/www/html/import/
There's a couple of ways I can think of off the top of my head:
Find out if your FTP server can be configured to trigger a script for you. (For example, pureftpd's upload-script function https://linux.die.net/man/8/pure-uploadscript , not all FTP server software can do this.)
Setup an inotify watcher perhaps with inotify-tools. (You could create your own with PHP as well (inotify extension), but that would probably negate any performance gain since you'd have an instance of PHP constantly running.)
And a sort-of third option: If you're merely wanting to avoid invoking PHP just to see that the file isn't there - you can chain to a bash file test before invoking your PHP script. The cron call still runs every 5 minutes, but only calls PHP if the file is there. (Note that I haven't exactly tested this, but pretty confident it would work.)
SHELL=/bin/bash
*/5 * * * * test -e /var/www/html/import/IMPORT-DATA.CSV && php /var/www/html/cron/import-csv.php
One way is you can distribute your tasks is 3 different scripts and chain them in cron.
*/5 * * * * php /var/www/html/cron/import-csv.php && /var/www/html/cron/YOUR_PERFORM_SOME_OTHER_TASK_PHP_SCRIPT && /var/www/html/cron/YOUR_DELETE-CSV-SCRIPT.php
&& will make sure that, the next script runs only if the previous
ran sucessfully.

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

As implied in the title, the Cron Job is supposed to execute a php file (update.php, to be specific). The php file then writes to a csv file stored in the same directory.
I have the time set to * * * * * so that it executes every minute. The command is written as follows:
php -q /home//public_html/wallboard/update.php
I don't believe this is causing any errors, though it also doesn't seem to write to the CSV file. When I visit update.php in a browser, however, it executes and writes to the CSV file immediately. I'm not experienced with Cron Jobs and I'm sure there's an issue, but I don't know what exactly that issue is. Let me know if you have suggestions/questions. Any help is appreciated!
Current Command:
* * * * * usr/bin/php -q /home/<user>/public_html/wallboard/update.php
update.php:
<?php
include('lib/HelpDeskView.php');
include('lib/WallboardDisplay.php');
include('helpdesk.csv');
$helpdesk = new HelpDeskView();
$text="\r\ntest,test,test";
file_put_contents( "helpdesk.csv" , $text, FILE_APPEND);
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
IMHO the best way is to contact support and ask them about command line syntax.
This is how I'm doing it at my linux server using cPanel.
This runs script.php which is stored in public root. Course, replace <username> in command line with your username.
At another server I'm using same command line with /usr/bin/php instead of php at the beginning of line, but I'm aware that not all servers use same command line. Some require php-cli in command line instead of php, some don't "like" -f argument, etc. So try various combinations.
To find more suggestions check out this SO topic too: Run a PHP file in a cron job using CPanel
Important thing: When trying different commands wait at least a minute (this case) to see if it works because Cron doesn't fire your script immediately.
Try to execute the same command in PHP CLI and check if it gives you any error, you might be missing some libraries or references required for CLI execution.
/usr/bin/php -d register_argc_argv=On /home/USERNAME/public_html/DOMAIN/artisan AMIR:HOME

PHP Cron job on MAMP / OSX

I've tried following the instructions here but I still can't get my cron job working on my mac using crontab.
I've saved the following cron job:
*/2 * * * * /usr/bin/php http://localhost/social-api-examples/cron.php
And when I type crontab -l it shows me it's saved. But my PHP page isn't being hit. Is there a way to check if it's working?
Also, when I saved the cron job, it was to a temporary file location. Is that ok?
The solution was just to use "php" instead of the path to php:
*/2 * * * * php http://localhost/social-api-examples/cron.php
/usr/bin/php is not an HTTP client: you need to pass it a file path not a URL.
Alternatively, if it is important to involve a web server, you can replace /usr/bin/php with an HTTP client such as curl or wget.
You should at least test the command you are trying to execute
You cannot execute a URL with php http://somesite.com/index.php
instead use wget if it's installed on your system:
wget http://somesite.com/index.php

Will cron execute php files or only CGI scripts?

Can .php be used in Crontab function on Linux or wil it execute only .CGI scripts?
I'm using Plesk Control panel, I did the settings as per the Crontab doc, but i think it's not executing the php files.
Does any one have the Idea about what more to do with
To add to the previous answers, yes crontabs can be used to execute php scripts.
You can either have them run through the php interpreter as Paul and fvu suggested, in which case you need to specify the correct path to the php interpereter ( get it in php by using exec('whereis php'); it will print out the path to php on your system ).
The alternative is to simply use wget to fetch the php file via http which in turn executes it.
* * * * * wget http://yoursite.com/yourscript.php
You can absolutely execute php script from cron.
Like this:
in the crontab:
*/5 * * * * /usr/bin/php5 -q /path/to/script/yourscript.php
Will execute yourscript.php every 5 minutes.
You can only make cron run executables. If you wish to run a PHP script, run php -f followed by the script's filename, e.g.:
/usr/local/bin/php -f script.php

Categories