Devner
Devner

Reputation: 7235

PHP: Error trying to run a script via Cron job

I have setup a cron job via my Control panel. I have uploaded the script via my FTP, set up it's permission as 777 (is it safe to do so?) & gave the path to this script in the job. Now the script makes use of dependent scripts to be able to run the job. Confusing? Here's what it's like:

cron.php

<?php require("some_file1.php");
require("file1.php");
require("folder1/file1.php");
require("folder1/file2.php");
require("folder2/file1.php");

//This value is actually received from one of the require files above after come calculations
$get_content = 'This is some value received after calculations.';

mail('Hi', '[email protected]', $get_content, 'Error');
?>

I have opted to receive Confirmation of the Cron job to my email & here's the error that I received:

mydomain.com/cron.php: line 1: syntax error near unexpected token `('
mydomain.com/cron.php: line 1: `<?php require("some_file1.php");
'

I tried talking to the support but they don't have any idea of this technical detail & currently the technical guys are not available. It will be great if someone can help me out here.

Looking forward for your replies.

Thank you.

Upvotes: 1

Views: 4532

Answers (5)

Devner
Devner

Reputation: 7235

I was able to solve this with the help of the tech support from the website. Here's the solution just in case anyone was wondering. The following is the "Command to run" & needs to be added via the Control Panel (GUI) of the website.

/usr/local/php5/bin/php 

/home/username/mydomain.com/cron.php

The cron.php file stays the same.

I guess I am going to have to accept my own answer as this is the most relevant answer which carries the perfect solution. I still do thank you all for your help & appreciate all the responses.

Upvotes: 0

MrWhite
MrWhite

Reputation: 45829

You need to use absolute paths in your script if you are using CRON (or at least the correct relative path*). The CWD is different when your script is run from the command line (ie. CRON). Although if you are not supplying any path then it should be using whatever the include_path is set to.

*You could change the CWD with chdir().

Also, try removing the brackets, ie.

require "some_file1.php";  // brackets are not reqd - it's a language construct

Upvotes: 0

GWW
GWW

Reputation: 44093

You could include the scripts using:

$path = dirname(__FILE__);
require($path."/"."file1.php"); 
...

This should resolve the relative path issue.

Upvotes: 0

BojanG
BojanG

Reputation: 1922

Try curl http://youdomain.com/path/script.php.

While it is not generally recommended it might be easier to generate http request using cURL or Wget. That way you avoid fishing for php CLI binary and include path.

Upvotes: 1

Mikhail Chernykh
Mikhail Chernykh

Reputation: 2697

I think, they have different configuration files for mod_php and command-line php. Another thing to check - try to add interpreter string to the top of php file:

for example:

 #!/usr/local/sbin/php

Upvotes: 7

Related Questions