Udders
Udders

Reputation: 6976

CRON job not executing and returning errors

I have the following code that I am running in a cron,

<?php
    $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
    $directory = $_SERVER["DOCUMENT_ROOT"]."/cron/";
    $file = "register_warranty_". date("dMY") .".csv";
    $filepath = $directory.$file;

    if(!file_exists($filepath)) 
    {
        $fp = fopen($filepath, "w+");
        fwrite($fp, "Title, Initials, Surname, HomeTel, Mobile, HouseNumber, Address, Address2, Town, Postcode, NewlyBuiltHome, InstallationDate, Houseowner, InstallersName, InstallersEmail, InstallersMobile, InstallersGasSafeNumber, BusinessName, BusinessAddress, BusinessAddress2, BusinessTown, BusinessRegion, BusinessPostcode, BusinessGasSafeNumber, BusinessEmail, SuppliersName, Model, serial, MaintenanceContract, ExpiryDate, Donotwishtobecontactedviapost, Donotwishtobecontactedviatelephone, Donotwishtobecontactedviaemail, Donotwishtobecontactedviasms\n");
        fclose($fp);
    }

?>

In the Cron email I get when the cron is run - I get the following errors,

line 1: ?php: No such file or directory
line 3: syntax error near unexpected token ('
line 3:
$_SERVER['DOCUMENT_ROOT'] = dirname(FILE);'

Am I doing something obviously wrong, I am not the most experienced with Cron work.

Upvotes: 0

Views: 127

Answers (2)

anubhava
anubhava

Reputation: 785186

You can do either of 2 things:

  1. Execute your PHP script from crontab as :

    /usr/bin/php -q script.php

  2. Add a shebang line at the start of your PHP script:

    #!/usr/bin/php -q

    And give execute permission to your script as:

    chmod +x script.php

Upvotes: 1

ynh
ynh

Reputation: 2939

I think you forgot to add the php command before the file name in the cronjob.

php myscript.php

Upvotes: 0

Related Questions