Search code examples
phplaravellaravel-artisansystemd

Handle() function in console command not being invoked


I am having problems with Laravel's console commands. My issue is that the handle() function is not being invoked automatically when running my scheduled jobs in the Console kernel using systemd (rather than crontab).

Let me try and clarify further what is going on:

  • The handle() function IS invoked automatically when calling 'php artisan my:command' from the console
  • The handle() function IS invoked automatically when my scheduled jobs are executed by calling artisan in the ctontab
  • The handle() function IS NOT being invoked automatically when my scheduled jobs are executed by calling artisan as a SYSTEMD task. The key difference here is that handle() is called when using crontab, but not when using systemd.

Does anybody with knowledge of systemd scheduling have an idea as to why this is? I can see from the system journal that my commands are being called by the systemd service, however, the handle() function is never invoked.

You might say, why not use crontab? However this is not an approach I can take and don't see a reason why it shouldn't work anyway. Help would be appreciated!

This is my systemd artisan.service:

[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 );

    }
}

Example Console command:

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();
    }
}

Solution

  • I now have the solution.

    The systemd service 'Type' should be set to 'forking' and not 'simple'.

    Therefore, changing the artisan.service to the following resolves the issue:

    [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
    

    Not to forget a 'systemctl daemon-reload' after changing the serivce.