tags:

views:

188

answers:

7

I have several PHP files to be run by cron. I set up the crons using command-

crontab crontab.txt

Inside the crontab.txt file, I have written cron commands like this:-

#(Updating tutor activities) - every minute
* * * * * /usr/bin/wget -O - -q -t 1 http://project/cron/tutor_activities.php

But none of the functionalities are working (database queries, sending reminder mails etc.). Running the URLs manually works.

Then I put my mail address in MAILTO and received the mails. In the mail, I received entire HTML source of the page. What is expected in the mail? Why are my functionalities not working?

Updates
If I change my cron commands to

 #(Updating tutor activities) - every minute
    * * * * * /usr/bin/wget http://project/cron/tutor_activities.php

Still no success and this comes in my mail -

--15:03:01--  http://project/cron/tutor_activities.php
          => `tutor_activities.php'
Resolving project... IP Address
Connecting to test.project|IP Address|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://project./ [following]
--15:03:01--  http://project./
          => `index.html.1'
Resolving project.... IP Address
Connecting to project.|IP Address|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://project/home/ [following]
--15:03:01--  http://project/home/
          => `index.html.1'
Resolving project... IP Address
Connecting to wproject|IP Address|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
index.html.1 has sprung into existence.
Retrying.

And lots of index.html.1 , index.html.2 files are accumulating in the root of my project. I do not want these files to be created. Just want the files to execute.

Same results if I use either of the two commands -

   * * * * * /usr/bin/wget http://project/cron/tutor_activities.php

   * * * * * wget http://project/cron/tutor_activities.php

running php command with MAILTO set sends me this error /bin/sh: php: command not found.

* * * * * php /path/to/test.php

So, I am not able to use php command.

I have written a simple mailto() inside my test.php. The mail does not come when run through cron (using both wget and php fails) but running the URL manually works.

My problem
To make it clear again, my main problem is that the functionality inside the cron files is not running. Creation of files is a secondary issue.

Any help would be appreciated

Thanks,
Sandeepan

+6  A: 

if you want to call an url as cronjob, you'll have to use somthing like wget. if it's a php-script on your server it would be easier to use php /...pathtomyscript.../cron/tutor_activities.php

oezi
@oezi, thanks for the answer, I am already using wget and the ` -O - -q -t ` part in my wget is for not outputting files as far as I remember. Please check my update. If I remove the ` -O - -q -t ` every cron run creates a file. I dont want that.
sandeepan
you could try to do "wget myurl > /dev/null" (or something like this, down't know the exact syntax) which would do a "normal" wget without optional parameters and "store" the autput to /dev/null
oezi
@oezi running `* * * * * php /path/to/test.php` with `MAILTO` set sends me this error `/bin/sh: php: command not found`. I have written a simple `mail()` inside my test.php. The mail does not come when run through cron (using both wget and php) but running the URL manually works.
sandeepan
maybe you'll have to give the whole path to the php-interpreter (like "/etc/php5/bin/php") instead of just writing "php"
oezi
run `which php` on the command line, then put that path into cron, as oezi suggested.
Frank Farmer
This is very tricky, When you'll execute php file directly, you are bypassing http server, so any functionality has to be independent on $_SERVER variable and $_COOKIES, $_SESSION.... .I'm usualy use wget...
palmic
+1  A: 

When you call wget with -O -, it will send the downloaded content to stdout, which cron is sending to you via the email message. In the first case, it's doing exactly what it should.

When you call wget witout the -O parameter, it will try to save the downloaded content as a file of the same name as the web page being downloaded. If it exists, it will add the incrementer to the name, as you saw. In this second case, it's doing exactly what it should.

It's not clear from your question where you want the output to go, but if you want to save the output to the same file each time, use -O myfilename.html.

dj_segfault
@dj_segfault I do not want these files to be created.I Just want the files to execute and produce the results. But since the results are not coming (database changes, mails not going).
sandeepan
OK, so much of your question was about the output of wget it wasn't clear that you don't even care about that. I would definitely proceed by adding some server-side logging debugging to tutor_activities.php. If it's getting called at all, calling it from wget either way shouldn't matter.
dj_segfault
+1  A: 

If your running PHP from cron/command line make sure you put the full path to the php executable

Petah
+1  A: 

It's entirely possible that PHP's not in the path within the cron environment - it's definitely not going to have the same setup as your regular shell. Try using the absolute path to BOTH the php interpreter AND the php script in the cron command:

* * * * * /path/to/php /path/to/test.php

As for the creation of files, you just have to add a redirect to your wget command:

wget -O - ... http://....     > /dev/null

-O - forces wget to write anything it downloads to standard output, which cron will then happily email to you. By adding the > /dev/null at the end of the command, this output will instead go the Great Bitbucket in the Sky. If you don't want wget's stderr output emailed either, you can also add a 2&>1 after the /dev/null, which further redirects stderr to stdout, which is now going to /dev/null.

Marc B
+1. Use `which php` to discover the full path to the PHP executable.
Frank Farmer
A: 

An easy and secure (no tmp files) way to do this is to use bash's process substitution:

* * * * * bash -c "/path/to/php <(/usr/bin/wget -O - -q -t 1 http://project/cron/tutor_activities.php)"

Process substitution runs the command within <() and puts the output into a file object that is only visible from the current process. In order to use it from cron, invoke bash directly and pass it as a command string.

And as others have mentioned, use the full path to php which you can find out with which php.

kanaka
+4  A: 

try

which php

The path which is returned should be placed with the command which is passed to run the Cron file.If you are setting up the Cron through Shell,it won't give any problem,but to be assured,try giving absolute path when you are trying to run a php page.

/path/to/php /path/to/cron/script/

Try to give your comand like this,if the problem persists;feel free to discuss.

Nishant Shrivastava
A: 

I found the problem myself. I did not put the same URL in my crontab file which I was running manually and that was my mistake.

While running manually I was just typing test in the URL, my browsers's list of saved URLs was appearing and I was selecting the URL http://www.test.project.com/cron/tutor_activities.php, but in the crontab file I had put http://test.project.com/cron/tutor_activities.php. I was mistakenly assuming this would run http://www.test.project.com/cron/tutor_activities.php (because we have a rewrite rule present to add www)

But the rewrite rule was redirecting it to http://www.test.project.com/home. That's why the HTML content in the mails.

So, the most important thing to learn here is to make sure we don't miss the minute things and don't assume that we did everything correctly. In my case, better to copy-paste the working URL into the cron file.

sandeepan