Question

I've set up a cron job to run. It executes a php file which is named cronj.php But it doesn't work and cron job notification I get is:

/root/website/myworld/blabla/cronj.php: line 1: ?php: No such file or directory

And line 1 in that file is simply a php tag <?php I don't know how

Was it helpful?

Solution

Cron is executing the file as if it was a shell script. Normally you would put in a shebang line (like #!/usr/bin/env php) at the top of the file so that the shell knows how to invoke it, but PHP doesn't like it - as it outputs everything outside its tags. Thus, instead of this:

0     3     *     *     *         /mypath/myscript.php ...

try this:

0     3     *     *     *         /usr/bin/env php /mypath/myscript.php ...

or use @Ravenex's trick.

EDIT I was just rightly admonished for assuming PHP behaves in a consistent way. Apparently, shebang does work in PHP. My apologies to @chess007.

OTHER TIPS

We use cron to run nightly tasks in a php facebook game. We do it by using curl like this:

/usr/bin/curl http://www.ourdomain.com/page.php

If I remember right we had some issues using localhost to try to avoid external lookups. Also we tried using php command line execution, which mostly worked but caused a few strange bugs.

Try to call the web url (http://.....).

It's apparently not parsing it as an PHP script.

Edit: Please show use the cronjob you used, to verify my hunch was right.

Use this to set your cron and also give email address in your cron setting Cpanel so that you get an email when cron runs successfully

wget -O - http://YOURSITE/cron.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top