过去3天我一直在为我的网站设置cron。在我的cpanel中,我提到了一个每5分钟运行一次的命令,命令是
wget http://www.example.com/artisan cron:run
在我的工匠中我添加了
Artisan::add(new CronRunCommand);
CronRunCommand.php是
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CronRunCommand extends Command {
protected $name = 'cron:run';
protected $description = 'Run the scheduler';
protected $timestamp;
protected $messages = array();
protected $runAt = '03:00';
public function __construct()
{
parent::__construct();
$this->timestamp = time();
}
public function fire()
{
$this->everyFiveMinutes(function()
{
// In the function, you can use anything that you can use everywhere else in Laravel.
$affectedRows = User::where('logged_in', true)->update(array('logged_in' => false)); // Not really useful, but possible
Artisan::call('auth:clear-reminders');
$this->messages[] = $affectedRows . ' users logged out';
});
$this->dailyAt('09:00', function()
{
Mail::send('hello', array(), function($message)
{
$message->to('admin@mydomain.com', 'Cron Job')->subject('I am still running!');
});
});
$this->finish();
}
protected function finish()
{
// Write execution time and messages to the log
$executionTime = round(((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000), 3);
Log::info('Cron: execution time: ' . $executionTime . ' | ' . implode(', ', $this->messages));
}
protected function yearly(callable $callback)
{
if(date('m', $this->timestamp) === '01' && date('d', $this->timestamp) === '01' && date('H:i', $this->timestamp) === $this->runAt) call_user_func($callback);
}
}
在我的电子邮件中,我收到此消息: 404未找到 2015-09-06 04:38:02错误404:未找到。
- 2015-09-06 04:38:02-- ftp://cron/run =&GT; “跑” 解决cron ...失败:名称或服务未知。 wget:无法解析主机地址“cron”
答案 0 :(得分:0)
这个工作非常好,我已经开始定期发送电子邮件 wget http://www.example.com/protected/app/controllers/TestController.php