I have the command, which works like php artisan queue:listen
. And it can't work in background in common, but I have to add it to cron tab, but it does not work there. Does it possible to do something like php artisan schedule:run
? The most imortant that when I interrupt this command, all functionalyty will stop. What do I have to do in this situation?

- 147
- 10
2 Answers
Laravel has his own cron. First of all, you should add Laravel cron to Linux system cron
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
then you can add your commands to Laravel cron.
Laravel crons lives inside a file /app/Console/Kernel.php the are should be inside function protected function schedule(Schedule $schedule) for example
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:send Taylor --force')->cron('* * * * *');
}
But if you want your command run as a system process not as a cron you should use supervisors program(supervisord) or you can create for PHP command file a systemd service file and then run as if the are a normal systemd service and even manage this service through monit program in with web interface as well

- 172
- 4
-
1Thank you, I think I will try to use Supervisor because it does not work in cron. – Aleks Apr 09 '21 at 05:41
-
Yes it is a good idea to use Supervisord and the are some information about it in the Laravel docs https://laravel.com/docs/8.x/queues#supervisor-configuration – Alexandr Blyakher Apr 09 '21 at 05:49
-
I tried to do it through Supervisor and it works well. Thank you. But if I change something in DB, it does not restart. Actually, I put autorestart into config. What can I do? Because when I change values of settings it will not change too while I do not use supervisorctl restart – Aleks Apr 09 '21 at 08:48
If your php script is a process it means that the constructor method of class runs only ones when you start your script and if you red db data in the constructor that data in the script would be stale Your process script should be process something like this
class OpenOrders extends Command
{
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->initTicker();
$this->updateBalances();
//the Process
while (true) {
//this is the right place to read DB data
$this->getAllOptions();
$this->openOrders = $this->getOpenOrders();
}
return 0;
}
}

- 172
- 4