Crontab php fail - php

My cron file says:
* * * * * root /usr/bin/php /var/www/html/cron.php
And cron.php is executable and located at /var/www/html/cron.php, still it doesn't work at all. My cron.php file:
<?php
include('Includes/top.php');
$Cron->closeServer();
?>

The problem is the include() is relative and you're probably not in the right working dir.
Change your command to cd /var/www/html && /usr/bin/php cron.php

Related

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");

Crontab is not working... Tried all possible solutions

I am trying to run an automated tasks from php script which I couldn't run. For testing purpose, I created test.php and still nothing is working.
These are the lines I executed in the flow
crontab -e
This opened nano with
#.............hint text were here
#.............hint text were here
* * * * * /usr/local/bin/php -f test.php
then I restarted the crontab
sudo service cron restart
confirmed the cron is working using
pgrep cron
and got the result. Also
/usr/local/bin/php test.php
also gave me Hello World in the terminal
Test.php
!#/usr/local/bin/php
<?php
echo "Hello World";
?>
The cronjob is also confirmed by executing
crontab -l
Tried to set permissions too
chmod +x test.php
chmod 755 test.php
chmod 600 test.php
Looking for a support to make it happen. Thanks.
Chances are your crontab is working but you just don't see the output because by default cron mails the standard and error output to the owner of the cron job. So what you can do is redirect them to a file and check it out manually.
You can do that by editing the crontab entry so that it looks like this:
* * * * * /usr/local/bin/php -f test.php > /tmp/log.log 2>&1
And then in a minute you can check it out to make sure your job is actually working.
I hope this helps.
EDIT
As it turned out in the comments of the question there was actually an error /bin/sh: 1: usr/local/bin/php: not found recorded in the log.log file which was fixed by just using php
Try doing the following
*/1 * * * * [username] /usr/local/bin/php -f test.php
e.g. */1 * * * * root /usr/local/bin/php -f test.php

EC2 Cron - Not Loading External File

I have built a program which loads values from an external file like so:
$terms = fopen('terms.csv','a+');
while($row = fgets($terms)) {
$termarr[] = urlencode($row);
}
This works on localhost, as well as when launched on my EC2 server remotely through the command line. However, when I run this through a cron job (*/5 * * * * ec2-user /usr/bin/php -q /var/www/html/loader.php), the file is not loaded and I get errors later on in my script due to $termarr containing 0 elements.
Add the following line to your cron :
*/5 * * * * ec2-user cd /var/www/html/ && /usr/bin/php -q loader.php
To make sure you are executing your script from /var/www/html
Last thing to checkout is the file's owner of your script, please be sure that ec2-user owns it, by chown it.
MrRhum.

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

Using CronTab to run php scripts

I need to send emails hourly and daily. I've tried nearly everything but it appears my crontab just won't work. If I run the scripts via a browser e.g
http://localhost/Maisha/Functions/sendhourlymails.php
my emails get sent beautifully.(I moved default website localhost to public_html.) I don't know whats wrong. I read some post in stack overflow including the executable path of php helps hence I've put the /usr/bin/php before the actual script to be cronned will work but it does not. Removing /usr/bin/php does not work. Adding php before the actual script isn't working.
I have the following entries in my crontab.
# m h dom mon dow command
0 * * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
0 0 * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/senddailymails.php
Try to call the script via http with wget like so:
* * * * * wget http://localhost/myscript >/dev/null 2>&1
Yeh, wget is good option, also you can try to use:
0 * * * * /usr/sbin/php /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
but it could work wrong due to relative paths.
Also you should look at http://php.net/manual/en/features.commandline.php
Try to put this into your .php file
<?php
#!/usr/local/bin/php -q
//your code here
?>
Then if you include any file into this file you must use something like:
include"/var/www/../your_absolute_path_from_root_folder/connect.php";
Finnaly make sure this file has the right permissions..Try
chmod 755 /var/www/.../file.php
Then if you edit your crontab file with the following command
vi /etc/crontab
put something like
10 6 * * * root php /var/www/..path../file.php
and restart the service with this command
/etc/init.d/cron restart
you have do your job!!
Note-Tip:the php file isn't neccessery to be into public_html
folder!!

Categories