6

I am getting the following three lines in an error message in /var/mail/username after the following job runs in crontab...

15 * * * * /Applications/MAMP/htdocs/iconimageryidx/includes/insertPropertyRESI.php

Errors:

/applications/mamp/htdocs/iconimageryidx/includes/insertpropertyRESI.php: line 1: ?php: No such file or directory
/applications/mamp/htdocs/iconimageryidx/includes/insertpropertyRESI.php: line 3: syntax error near unexpected token `'initialize.php''
/applications/mamp/htdocs/iconimageryidx/includes/insertpropertyRESI.php: line 3: `require_once('initialize.php');

The PHP script I am trying to execute DOES in fact exist, and I have made absolutely sure the spelling is correct several times. I ran a crontab on another script before and it worked just fine...any ideas??

The 2nd & 3rd Errors are from line 3 in the following script (the one I am trying to run with the crontab):

<?php
    require_once('initialize.php');
    require_once('insertPropertyTypes.php');

    $sDate;
    if(isset($_GET['startDate'])) {
        $sDate = $_GET['startDate'];
    } else {
        $sDate = '';
    }

    $insertResi = new InsertPropertyTypes('Listing', $sDate, 'RESI');
?>

When I run my script insertPropertyRESI.php in the browser, it runs just fine???? Also, initialize.php and insertPropertyTypes.php are in the same directory as insertPropertyRESI.php

I am using MAMP with PHP 5.3.5

thakns for the help :?

5
  • what does your cron entry look like? I have a feeling you're not prepending the php on your command...
    – qweet
    Jun 22, 2012 at 13:12
  • What's the exact line for this task in your crontab file? Jun 22, 2012 at 13:12
  • my bad, just edited the post 15 * * * * /Applications/MAMP/htdocs/iconimageryidx/includes/insertPropertyRESI.php Jun 22, 2012 at 13:13
  • 2
    Try running php -f /Applications/MAMP/htdocs/iconimageryidx/includes/insertPropertyRESI.php on the commandline. If that works, then use that in the cron.
    – qweet
    Jun 22, 2012 at 13:16
  • @qweet is there any way I can specify a different PHP installation, like /Applications/MAMP/bin/php5.3/bin/php ? Jun 22, 2012 at 18:25

3 Answers 3

11

You are trying to execute a script which does not have a valid shebang.
Add the shebang as the first line of the script like this (adjust path of course):

#!/usr/bin/php

Or change the crontab entry to:

15 * * * * /usr/bin/php /Applications/MAMP/htdocs/iconimageryidx/includes/insertPropertyRESI.php
1
  • I am having an issue with that installation of PHP (see my comment below). I tried using the path to MAMP's PHP installation at /Applications/MAMP/bin/php5.3/bin/php -- for both the shebang and as a prefix in the job entry itself...no luck...I will post the errors in a moment Jun 22, 2012 at 14:39
3

The reason why it runs in your browser is because your server is configured to automagically compile and serve PHP files.

Cron on the other hand, doesn't have that luxury; by default it will run anything that you put into it as a bash script.

From the looks of your Cronjob, you're running the PHP file without using the php parser, and as such Cron tries to run the script as a bash script.

If you put php -f in front of the location of the PHP script, you are telling Cron to run the command using the php parts, you should find that it will now run.

Alternatively, you could edit your code and prepend #!/usr/bin/php to the beginning of the script. When Cron runs this file, it will use php to run the command, instead of bash.

2
  • When I use php -f (loading from /usr/bin/php) I get some output that shows that the script begins running but hits a fatal error with the message: file {$class_name} could not be found . Since I am using mamp, I tried using the path to MAMP's installation of PHP: /Applications/MAMP/bin/php5.3/bin/php -f /path/to/script.php and THIS method caused the script to execute...still no luck with the crontab though, will post errors in a minute. To make sure, I DO or DO NOT include the -f if I set the php path INLINE and not in a #! shebang? Jun 22, 2012 at 14:37
  • 1
    this is ancient but it should read #!/usr/bin/php, not !#/usr/bin/php
    – user101289
    Nov 21, 2017 at 19:49
2

Running a php file on the command line is different than running it within web server like apache. To run a php script on the command line, you need to:

  1. Install the package php-cli as this may not be installed by default.
  2. Make sure you have the comment '!#/usr/bin/php' in the first line and make sure you set 'x' permission on your php script.
  3. Alternatgively to 2, you just run the script by calling it using 'php your_script'
6
  • The php is installed, as it comes pre-installed with MAMP -- I am using a MAMP setup. It is installed in /Applications/MAMP/bin/php5.3/bin/php. Is there any way I can use this installation in my crontab instead of the one in /usr/bin/php? The one in /usr is older, version 5.2, and is giving me an error that I am not getting with PHP 5.3. I did run the script with php -f /my/script and got the error I mentioned file {$class_name} could not be found; when I run the script using the MAMP php path, the error goes away and the script executes, but the crontab doesn't like it :? Please advise Jun 22, 2012 at 14:51
  • Simply specify the full path when calling your script like /Applications/MAMP/bin/php5.3/bin/php /path/to/your/script.php.
    – Khaled
    Jun 22, 2012 at 20:10
  • I tried that, but it appears that it is not executing at all. Currently, my only method of checking for crontab executions is through the mail notifications; I don't get any mail when I use that method. Is there an alternate method that I can use to check for errors or for other information about my cron jobs? Jun 22, 2012 at 20:15
  • 1
    Here is the exact line: */1 * * * * /Applications/MAMP/bin/php5.3/bin/php /Applications/MAMP/htdocs/iconimageryidx/includes/insertPropertyRESI.php Jun 22, 2012 at 20:20
  • Can someone that can edit only a few characters fix the typo? "Alternatgively"
    – d_rail
    Apr 6, 2018 at 0:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .