3

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

Johan
  • 74,508
  • 24
  • 191
  • 319
Ilja
  • 44,142
  • 92
  • 275
  • 498
  • Show us the entry in your crontab. – Maerlyn Dec 24 '11 at 21:19
  • possible duplicate of [shebang #! does not work in ubuntu linux when created in Windows 7](http://stackoverflow.com/questions/5383960/shebang-does-not-work-in-ubuntu-linux-when-created-in-windows-7) – mario Dec 24 '11 at 21:44

4 Answers4

7

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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Hm, indeed, you are correct. I tested CLI with standard input, and foolishly believed it would behave the same as with a filename argument. – Amadan Dec 24 '11 at 21:35
  • so no my path would look like this?: /usr/bin/env php /root/website/myworld/blabla/cronj.php – Ilja Dec 24 '11 at 21:48
  • That looks correct. Or, as I have been told, you can keep your cron entry as is, and put `#!/usr/bin/env php` as the first line in your PHP script. – Amadan Dec 24 '11 at 21:50
  • Ok I'll test both solutions now. – Ilja Dec 24 '11 at 21:52
4

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.

Randy
  • 9,419
  • 5
  • 39
  • 56
Ravenex
  • 621
  • 3
  • 10
  • We also found this allows for easier debugging because it puts the script in a place where it can be tested from a browser and debug information can be readably outputted. Though with this solution be careful to think about security so that outside users can't stumble upon and run the script. – Ravenex Dec 24 '11 at 21:33
1

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.

Derk Arts
  • 3,432
  • 2
  • 18
  • 36
0

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

Gyan
  • 498
  • 6
  • 10