开发者

PHP: Error trying to run a script via Cron job

开发者 https://www.devze.com 2023-01-09 00:14 出处:网络
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

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@mydomain.com', $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.


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


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.


You could include the scripts using:

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

This should resolve the relative path issue.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消