tags:

views:

131

answers:

5

Hi all,

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.

+5  A: 

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
netme
+1 I think the missing `#!/usr/local/sbin/php` is the problem. Note, you could also solve this by making your cron command call php with the file as an argument. Lastly, be aware that the php binary might live at a different location, like `/usr/bin/php`.
grossvogel
Thanks for your reply. Here's what I got when I added the line that you gave:`sh: mydomain.com/cron.php: /usr/local/sbin/php^M: bad interpreter: No such file or directory`. What can we do to fix it?
Devner
First: ^M looks like a bad line-ending character (win \r\n instead of unix \n). Set your text editor to use unix line endings, or use the `dos2unix` command. If that doesn't fix it, it could be that your php binary lives elsewhere on the server. If you have shell access, use `which php` to find it.
grossvogel
I think the path given above is just an example - you need the actual path to where the PHP interpreter is located on _your server_. But, as mentioned by @grossvogel, you don't necessarily need to include the [shebang](http://en.wikipedia.org/wiki/Shebang_%28Unix%29) line if you are calling the PHP interpreter directly in your CRON call.
w3d
A: 

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.

BojanG
A: 

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
w3d
Hi, Here's what I got when I simply removed existing tags at the top `
Devner
w3d
@w3d You are right about the tags.. Sorry, I misread what you said. I finally solved it. I am posting the solution below as a comment.
Devner
A: 

You could include the scripts using:

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

This should resolve the relative path issue.

GWW
A: 

Hi all,

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.

Devner
The path you see there is very dependent upon the particular way php is installed on your system. One way to make it more generic, so you don't need to go looking for the variable, is to use the following, without the quotes: "#!/usr/bin/env php"That will take care of finding the executable in your path, wherever it is (if it's in the path). If you want to figure out what to put in the line you can run, from a shell, which php # => /usr/local/php5/bin/php
Paul Rubel
Glad you got it resolved and thanks for reporting back. So, presumably you were initially trying to call your `cron.php` script directly using CRON without calling the PHP interpreter? And ultimately it was knowing the correct path to the PHP interpreter? Did you need to change the files permissions in the end?
w3d
Devner