Aleks
Aleks

Reputation: 147

How can I run in background command, which runs only in foreground?

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?

Upvotes: 0

Views: 1556

Answers (2)

Alexandr Blyakher
Alexandr Blyakher

Reputation: 180

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

Upvotes: 0

Alexandr Blyakher
Alexandr Blyakher

Reputation: 180

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

Upvotes: 1

Related Questions