Will cron execute php files or only CGI scripts? - php

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

Related

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

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

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 .

what to change in my php script for the cron job to work

Ok I have been looking into cron jobs for hours, checked every post here, looked in google but I just do not understand how it works.
I have set up a cron job using my path 1 * * * * /home/myuser/domains/mysite/public_html/live.php I have also tried /home/myuser/public_html/live.php
Nothing seems to be working.
Do I have to add something in the php file (live.php)? That is the code that has to be executed. The code itself works.
I know you will all think that I am lazy but I really can't figure this out.
*.php is regular script file which, as any other scripting languages like i.e. perl requires interpreter to run. So if you want to run your script from command line you have either call interpreter and give it your script file as argument, like:
$ /usr/bin/php myscript.php
And that's it - it should run.
Or (if working using linux/bsd) add as very first line of your PHP script file:
#!/usr/bin/php -q
which tells shell, where to look for interpreter for this script file. Please ensure your PHP is in /usr/bin folder as this may vary depending on the distro. You can check this using which, like this:
$ which php
/usr/bin/php
if path is right, you yet need to set executable bit on script file so you'd be able to try to "launch it":
chmod a+x myscript.php
This will make it behave as any other app, so you'd be able to launch it this way:
/full/path/to/myscript.php
or from current folder:
./myscript.php
And that's it for that approach. It should run.
So your crontab line would look (depending on the choosen approach):
1 * * * * /full/path/to/myscript.php
or
1 * * * * /usr/bin/php -q /full/path/to/myscript.php
And you should rather use "0" not "1", as 1st minute in hour is zero, i.e.:
0 * * * * /usr/bin/php -q /full/path/to/myscript.php
EDIT
Please note cron working directory is user's home directory. So you need to put that into consideration, which usually means using absolute pathes. Alternatively you'd prepend your call with cd <script working path> && /usr/bin/php -q /full/....
Maybe
1 * * * * php /home/myuser/domains/mysite/public_html/live.php
You need to make your script executable with chmod on the command line.
Furthermore you need something like this:
#!/usr/bin/php
<?php
// here comes your code
?>
This is required to tell the command line how to execute the file.
Note: this will only work if your php binary is located in /usr/bin/php

PHP Cronjob does not execute

I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.

Categories