Set path in /etc/crontab for php file - php

Hello I have to run cron which I have written in core php.
Path of my php file in
/var/www/html/xplore/crons/examples/example.php
when I go to path of mu file and run script using php example.php it give me proper output.
But How to give path of file in crontab I have tried it by using
*/2 * * * * root usr/bin/php xplore/crons/examples/example.php
Do you have any idea please help me in to that?

try like below,
*/2 * * * * /usr/bin/php -q /xplore/crons/examples/example.php
*/2 * * * * It means that cron will run in every 2 min.

Seems like the file path that you are passing is wrong. You can try this
*/2 * * * * root /usr/bin/php /xplore/crons/examples/example.php
if this path(xplore/crons/examples/example.php) is correct

Related

Shell Script can be executed from terminal but not from the crontab

I have to export products in the .csv or .txt file automatically all the time for the price compare feeds.
First, I was able to export products only via URL like this:
https://site-name.com/Process/Cron.php?run=1&engine=ExportProducts&service=1&profile_id=90&export_file=testcron_export.csv
But, I needed to export the products automatically on some time interval and for that I found a way to run the URL for exports of products in .csv or .txt file from terminal with the following command:
REQUEST_URI=‘Process/Cron.php’ SCRIPT_NAME=‘Process/Cron.php’ php-cgi -f Process/Cron.php run=1 engine=ExportProducts service=1 id=90 export_file=testcron_export.csv
And the products were exported successfully.
But, when I am setting up this command in the crontab, it’s not executed.
I tried using different ways, one of the ways is:
Copied the command (from above) in bash script and executed it successfully from terminal, but when I set-up the bash script in the crontab it's not executed.
These are all the combinations that I’ve tried in the crontab:
*/2 * * * * REQUEST_URI=‘Process/Cron.php’ SCRIPT_NAME=‘Process/Cron.php’ php-cgi -f Process/Cron.php run=1 engine=ExportProducts service=1 id=90 export_file=testcron_export.csv
*/2 * * * * ./testcron2.sh
*/2 * * * * ~/www.site_name.com/testcron2.sh
*/2 * * * * sh /home/site_name/SspStore/testcron2.sh
*/2 * * * * sh testcron2.sh
*/2 * * * * /bin/sh testcron2.sh
*/2 * * * * /bin/sh /home/site_name/SspStore/testcron2.sh
*/2 * * * * php-cgi -f REQUEST_URI=‘Process/CoreCron.php’ SCRIPT_NAME=‘Process/Cron.php’ Process/Cron.php run=1 engine=ExportProducts service=1 id=90 export_file=testcron_export.csv
*/2 * * * * usr/bin/sh testcron2.sh
*/2 * * * * usr/bin/sh /home/site_name/SspStore/testcron2.sh
PS. The shell script (testcron2.sh) is in the home directory of the shop.
Maybe the problem is with php-cgi that cannot be executed from terminal or I am missing something?
Thank you very much in advance

Execute PHP in cron job on centos

I needed help on my cronjob experiment, im unable to accomplish what i wanted. below are the things I did.
Create a php file inside /var/www/html/bob/test/index.php
Set permission rights of the directory and file to executable (0777)
Specify a cronjob
My index.php
<?php
$myfile = fopen("testfile.txt", "w");
?>
My various tested parameters for crontab -e which didn't work
*/1 * * * * root /usr/bin/php /var/www/html/bob/test/index.php
*/1 * * * * root /var/www/html/bob/test/index.php
*/1 * * * * root php -q /var/www/html/bob/test/index.php
*/1 * * * * root /var/www/html/bob/test/index.php
looking at some of my /var/log/cron I dont see any error indication.
Sep 6 15:55:01 localhost CROND[4866]: (root) CMD (root /var/www/html/bob/test/index.php)
Sep 6 15:56:01 localhost CROND[4872]: (root) CMD (root /var/www/html/bob/test/index.php)
Sep 6 15:57:01 localhost CROND[4878]: (root) CMD (root /var/www/html/bob/test/index.php)
Lastly, when i run my php via #php /var/www/html/bob/test/index.php the code executes fine and creates that textfile.
You need to remove root from your cronjob. The log is showing it's trying to execute a program called root with /var/www/html/bob/test/index.php as a parameter.
Instead, use the following as your cronjob:
*/1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
This answer states that if you want to set the user for a cronjob to be run, you have to edit /etc/crontab directly, not by using crontab -e.
1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
if this not working, so you have a probleme of permission , try to use chown and chmod for giving the right permission
and for $myfile = fopen("testfile.txt", "w"); try to use the absolute path for the file testfile.txt like :
$myfile = fopen("/home/xxxx/testfile.txt", "w");

Cron job not executing which are not in root folder

I have some .php files, some are in root foler and some are inside a directory say "cron_jobs" and I have added cron jobs for these files on server like as below :
*/2 * * * * php /home/public_html/mysite/demo_root.php
*/2 * * * * php /home/public_html/mysite/cron_jobs/demo.php
There are two cron job, where first cron job executing properly but second cron job not executing from server.
And "cron_jobs" directory has permission 755.
Why cron job not executing which are not in root folder?
Is there in any setting for this or Am I doing something wrong to add cron job?
I changed cron job from :
*/2 * * * * php /home/public_html/mysite/cron_jobs/demo.php
To
*/2 * * * * cd /home/public_html/mysite/cron_jobs/ php demo.php
and It is working for me.

How to run a cron job on OSX for php using MAMP

I've been at this for hours and have tried everything with no luck.
I am basically trying to run this http://docs.phpservermonitor.org/en/latest/install.html#setting-up-a-cronjob
I'm using MAMP and my localhost is a custom folder under User/username/localhost/servercheck
What I've tried so far is.
crontab -e
added */1 * * * * root /usr/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
And when I type crontab -l to see if its loaded I see the following.
*/1 * * * * root /usr/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php
But the script does not run. I even tried a simple script that writes to a file. Still nothing. For some reason the cron job doesn't execute. Any ideas?
You need to call MAMP’s PHP executable, which will depend on on the version of PHP you’re running. For 7.2.1, the following below would be the proper path
*/1 * * * * /Applications/MAMP/bin/php/php7.2.1/bin/php /Users/clientsupport1/localhost/servercheck/cron/status.cron.php

Using Relative Paths in Crontab

I am attempting to use relative paths in my crontab file on CentOS 6.4, so that I do not have to repeat the same absolute path over and over again. At the top of my crontab file, located here: /etc/crontab, I have:
SHELL=/bin/bash
PATH=/var/www/html/crons
MAILTO=""
HOME=/
And each of my commands looks like:
*/2 * * * * root /usr/bin/php "cronfile.php" >> "logs/cronfile_"`date +\%Y\%m\%d`".log"
I'm expecting that it'll run the cronfile.php PHP file in the /var/www/html/crons directory, and save the output from this to /var/www/html/crons/logs/cronfile.log. However, the file is not being run and the log file is not being created.
The command works fine if I run just:
/usr/bin/php "cronfile.php" >> "logs/cronfile_"`date +\%Y\%m\%d`".log"
from the command line after cding into the /var/www/html/crons directory.
Please advise, thanks.
After many trials and research, I discovered that the solution was using the HOME= variable, not the PATH= variable, like so:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/var/www/html/crons
And then each of the lines would just look like:
*/2 * * * * root /usr/bin/php cronfile.php >> logs/cronfile_`date +\%Y\%m\%d`.log
Hope this helps someone else with the same issue I had in the future.
/usr/bin is already in the PATH on most systems by default, so you should be able to remove the PATH declaration from the top of your crontab.
Your job is running in a bash shell so you could do something like:
*/2 * * * * root cd /var/www/html/crons && php cronfile.php >> cronfile_`date +\%Y\%m\%d`.log

Categories