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");
Related
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
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
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.
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
I am kinda newbie to this, I have a php script which I want to execute after every 1 hour, for this I updated the crontab file inside the /etc directory but I am not able to see whether its actually being called.
Here's the entry in my crontab file
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
*/5 * * * * /usr/bin/curl -o temp.txt http://myurl.com/postparser.php
I am not even able to see any temp.txt file getting generated
Can someone point me in the right direction
try running
*/5 * * * * /usr/bin/php /path/to/php/file/postparser.php
Change the /usr/bin/php part to your php executable path.
Not sure about this but, changing just the file would have no effect.
Try editing the tasks using the command crontab -e ("e" for editing).
If you want to edit the crontab for a certain user, use the -u parameter
for more, check the man:
http://linux.die.net/man/1/crontab
Good luck