Php 未调用console命令中的Handle()函数
我对Laravel的控制台命令有问题。我的问题是,在控制台内核中使用systemd(而不是crontab)运行计划的作业时,不会自动调用handle()函数 让我尝试进一步澄清正在发生的事情:Php 未调用console命令中的Handle()函数,php,laravel,laravel-artisan,systemd,Php,Laravel,Laravel Artisan,Systemd,我对Laravel的控制台命令有问题。我的问题是,在控制台内核中使用systemd(而不是crontab)运行计划的作业时,不会自动调用handle()函数 让我尝试进一步澄清正在发生的事情: 从控制台调用“php artisan my:command”时,会自动调用handle()函数 当通过调用ctontab中的artisan执行我的计划作业时,会自动调用handle()函数 通过将artisan作为SYSTEMD任务调用来执行计划作业时,不会自动调用handle()函数。 这里的关键区别
- 从控制台调用“php artisan my:command”时,会自动调用handle()函数
- 当通过调用ctontab中的artisan执行我的计划作业时,会自动调用handle()函数
- 通过将artisan作为SYSTEMD任务调用来执行计划作业时,不会自动调用handle()函数。 这里的关键区别在于,在使用crontab时调用handle(),而在使用systemd时不调用
[Unit]
Description=Artisan service
After=network.target
[Service]
Type=simple
Environment=TLNN=/opt/tlnn
ExecStart=/usr/bin/php /srv/www/tlnn/artisan schedule:run
[Install]
WantedBy=multi-user.target
Kernel.php:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Classes\InfoCommunications;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\SendData::class,
\App\Console\Commands\RefreshData::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule( Schedule $schedule )
{
$schedule->command( 'TLNN:SendData' )->cron( '*/1 * * * *' );
$schedule->call( 'TLNN:RefreshData' )->twiceDaily( 6, 18 );
}
}
控制台命令示例:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Classes\TlnnInfoCommunications;
class SendData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'TLNN:SendData';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Description here...';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$tlnnInfoCommunications = new TlnnInfoCommunications();
$tlnnInfoCommunications->Run();
}
}
我现在有了解决办法 systemd服务“Type”应设置为“forking”而不是“simple” 因此,将artisan.service更改为以下内容可以解决此问题:
[Unit]
Description=Artisan service
After=network.target
[Service]
Type=forking
Environment=TLNN=/opt/tlnn
ExecStart=/usr/bin/php /srv/www/tlnn/artisan schedule:run
[Install]
WantedBy=multi-user.target
不要忘记更改服务后的“systemctl守护程序重新加载”。您是否考虑过在
Kernel.php
?@ArminSam是的,我在Kernel.php中安排了任务。当从systemd运行时,这些就是问题所在