Wraith
Wraith

Reputation: 501

How can i run script, command on background and kill for request?

I would like run script with infiniti loop inside, all the time after server will restart. i created command eg.

<?php
    namespace App\Console\Commands;
    use Illuminate\Console\Command;
    class startParser extends Command
    {
        protected $signature = 'start:parser';
    }
?>

now i can run command: php artisan start:parser but is not in background.

Maybe i can use exec('nohup php artisan start:parser > /dev/null &'); or Artisan::call('start:parser'); or Artisan::call('start:parser'); but how can i get ID of this process and kill script when required ?

Upvotes: 0

Views: 744

Answers (1)

SviesusAlus
SviesusAlus

Reputation: 437

You can not separate a command outside of PHP thread when doing shell_exec() or anything else.

If you want to run that command when server boots up and keep it running no matter what you should have a look at how Laravel is executing queue workers. This will give you idea of how to do that.

https://laravel.com/docs/7.x/queues#supervisor-configuration

With Supervisor you can have any command up and running all the time. It actually is capable of restarting a command if it crashes.

You can get PID then with pgrep -f php when php is filter to specific name. For example

pgrep -f start:parser

Upvotes: 1

Related Questions