简体   繁体   English

在Windows上使用crontab运行php脚本不起作用

[英]Running php script using crontab on Windows wont work

If I set script on browser, everything is ok. 如果我在浏览器上设置脚本,一切正常。 Code from crontab 来自crontab的代码

* * * * * c:\xampp\php\php.exe -q c:\xampp\htdocs\abc\sleep.php

Php scripts PHP脚本

sleep.php sleep.php

<?php
require_once( "./sleep2.php");
$abc = new Registration();
$abc->register();   
?>

sleep2.php sleep2.php

<?php
    require_once("./config.php");
    class Registration
    {
        private $spojenie;

        function __construct()
        {
            $this->spojenie = new mysqli(db_host, db_user, db_pass, db_database);
        }

        public function register()
        {
            $insertPlayer = $this->spojenie->query("insert into skuska(nick,           rank) values('shock',12,12)");
            return true;
        }
    }

?>

And one more question. 还有一个问题。 If I open php.exe (command line window) and put there some command (-h), nothing happen. 如果我打开php.exe(命令行窗口)并放置一些命令(-h),则什么也没有发生。 Is it ok? 可以吗

Maybe, your php.exe doesn't use appropriate configuration? 也许您的php.exe没有使用适当的配置? Try c:\\xampp\\php\\php.exe -c C:\\path\\to\\php.ini c:\\xampp\\htdocs\\abc\\sleep.php 尝试c:\\ xampp \\ php \\ php.exe -c C:\\ path \\ to \\ php.ini c:\\ xampp \\ htdocs \\ abc \\ sleep.php

Tip: type php.exe --ini to get list of all configuration files you php interpreter will look into. 提示:键入php.exe --ini以获得php解释器将研究的所有配置文件的列表。

And I have no idea what -q flag does. 而且我不知道-q标志做什么。

php --help | egrep -e '-q'

produces nothing on my Ubuntu. 在我的Ubuntu上什么也没产生。

And just like Sarwar Erfan , don't have a clue how you run cronjobs on Windows. 就像Sarwar Erfan一样,不知道如何在Windows上运行cronjobs。 Maybe you should try Windows Schedule Manager instead? 也许您应该改用Windows Schedule Manager? It supports flags. 它支持标志。

Not sure how it works in Windows, but in Linux cron runs tasks from the user's homedir. 不确定它在Windows中如何工作,但是在Linux中cron从用户的homedir运行任务。 So, the current working directory for sleep.php won't be the directory where this file being stored. 因此,sleep.php的当前工作目录将不是该文件的存储目录。 You have to use absolute paths in your scripts in this case. 在这种情况下,您必须在脚本中使用绝对路径。 I think that FILE or DIR will help you out. 我认为FILEDIR将帮助您。 Take a look here http://php.net/manual/en/language.constants.predefined.php for details 看看这里http://php.net/manual/en/language.constants.predefined.php了解详细信息

Ensure you are in the correct directory by calling chdir(dirname(__FILE__)); 通过调用chdir(dirname(__FILE__));确保您在正确的目录中chdir(dirname(__FILE__)); in your script before including anything. 在您的脚本中,然后再包含任何内容。 Otherwise the require() call is likely to fail. 否则, require()调用可能会失败。

So 1, I changed crontab to: 因此1,我将crontab更改为:

c:\xampp\php\php.exe -c C:\path\to\php.ini c:\xampp\htdocs\abc\sleep.php

2, php.exe --ini 2,php.exe --ini

returns Configuration File (php.ini) Path: C:\\Windows 返回配置文件(php.ini)路径: C:\\Windows

Loaded Configuration File: C:\\xampp\\php\\php.ini 加载的配置文件: C:\\xampp\\php\\php.ini

Scan for additional .ini files in (none) 扫描(无)中的其他.ini文件

Additional .ini files parsed (none) 已解析其他.ini文件(无)

3, I changed require to include(dirname(__FILE__)."....." 3,我更改了要求include(dirname(__FILE__)."....."

4, It works :D :D :D :D 4,起作用:D:D:D:D

5, Im thinking about stop using require. 5,我正在考虑停止使用需求。

Thanks to all. 谢谢大家。 You really helped me. 你真的帮了我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM