The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP script as Cron job

Discussion in 'General Discussion' started by pphas, Feb 6, 2008.

  1. pphas

    pphas Member

    Joined:
    Jul 6, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    I want to execute a php script regularly, and for that reason I created a cron job through WHM. The file is indeed executed, but it fails to do so correctly. I receive this error:

    /home/site/cron.php: line 1: ?php: No such file or directory
    /home/site/cron.php: line 3: =: command not found
    /home/site/cron.php: line 4: =: command not found

    etc.

    After searching I find this has to do with the format of the script. I wrote this as if it is made for usage through a browser, but now I read that it should be adapted in order to work as a cron. However, I can not find how to change this. Anyone any ideas?
     
  2. troxalias

    troxalias Well-Known Member

    Joined:
    Nov 21, 2001
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Athens - Greece
    Your cron entry for the script should be something like:

    0 0 * * * php /home/site/cron.php

    which will execute with php the script /home/site/cron.php every day on 00.00 exactly.
     
  3. pphas

    pphas Member

    Joined:
    Jul 6, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Thanks for your reply. However, the cron appears to run just fine. The problem is that when the file is actually executed, I receive an error. It is this error that I do not understand...
     
  4. troxalias

    troxalias Well-Known Member

    Joined:
    Nov 21, 2001
    Messages:
    96
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Athens - Greece
    Your problem is that your system does not know how to run /home/site/cron.php . This file is not a binary file, it needs to be interpreted with php (as other need perl, python, etc) so the system HAS to know how to run it.

    In theory it would be enough to insert a Shebang line in your script, but this would case error from the php interpreter itself.
     
  5. pphas

    pphas Member

    Joined:
    Jul 6, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    1
    Thanks a lot! This was indeed the issue, I added the path to php and it all works fine now...

    For whoever battles the same issue: I added

    #! /path/to/php

    before the script and all is fine now.
     
Loading...

Share This Page