Cron Job Confusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    Cron Job Confusion

    Hey guys, here is my PHP code I want to run in a cron job:
    Code:
    <?php
    mysql_connect ("localhost", "username", "userpass") or die ('I cannot connect to MySQL because: ' . mysql_error());
    echo('Connected to MySQL<br />');
    
    mysql_select_db ("dbname") or die('I cannot connect to the database because: ' . mysql_error());
    echo('Connected to Database<br />');
        
    mysql_query( "
        UPDATE users...
        " ) or die(mysql_error());
    ?>
    I get the following error in the output being sent to my email:
    Code:
    /home/user/public_html/.../file.php: line 1: ?php
      : No such file or directory
      /home/user/public_html/.../file.php: line 2: syntax error near unexpected token `"localhost",'
      /home/user/public_html/.../file.php: line 2: `mysql_connect ("localhost", "username", "userpass") or die ('I cannot connect to MySQL because: ' . mysql_error()); '
    I know it's running, but I don't know why it's not working? There is no update so it's not getting to the function, but the path is all correct? Does that first erro mean I should not have <?php in my cron job file?
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    I'm guessing the problem is that the code is not being executed as PHP, but rather as shell commands.

    You need tell the shell that the code is meant to be executed using the PHP parser.
    That can be done by adding the path the the PHP parser as the first line of the file:
    [code=php]
    #!/usr/bin/php

    <?php
    // Do something.
    ?>[/code]
    If you don't know the path the the PHP executable, try doing:
    Code:
    whereis php
    That should show you where it is.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Thanks Atli. I spoke with support and they said a very similar thing to what you said. Although I cannot try it right now, it does make sense what you (and they) said, so thanks for your reply.

      Comment

      Working...