简体   繁体   中英

Running php script using crontab on Windows wont work

If I set script on browser, everything is ok. Code from crontab

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

Php scripts

sleep.php

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

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. Is it ok?

Maybe, your php.exe doesn't use appropriate configuration? Try 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.

And I have no idea what -q flag does.

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

produces nothing on my Ubuntu.

And just like Sarwar Erfan , don't have a clue how you run cronjobs on Windows. Maybe you should try Windows Schedule Manager instead? It supports flags.

Not sure how it works in Windows, but in Linux cron runs tasks from the user's homedir. So, the current working directory for sleep.php won't be the directory where this file being stored. You have to use absolute paths in your scripts in this case. I think that FILE or DIR will help you out. Take a look here http://php.net/manual/en/language.constants.predefined.php for details

Ensure you are in the correct directory by calling chdir(dirname(__FILE__)); in your script before including anything. Otherwise the require() call is likely to fail.

So 1, I changed crontab to:

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

2, php.exe --ini

returns Configuration File (php.ini) Path: C:\\Windows

Loaded Configuration File: C:\\xampp\\php\\php.ini

Scan for additional .ini files in (none)

Additional .ini files parsed (none)

3, I changed require to include(dirname(__FILE__)."....."

4, It works :D :D :D :D

5, Im thinking about stop using require.

Thanks to all. You really helped me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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