我如何在后台运行脚本、命令和请求终止



我想在服务器重新启动后一直运行带有无限循环的脚本。我创建了命令,例如

<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
class startParser extends Command
{
protected $signature = 'start:parser';
}
?>

现在我可以运行命令:php artisan start:parser,但不在后台。

也许我可以使用exec('nohup php artisan start:parser > /dev/null &');Artisan::call('start:parser');或Artisan::call('start:parser'(;但我如何获得这个进程的ID并在需要时杀死脚本?

在执行shell_exec()或其他操作时,不能在PHP线程之外分离命令。

若您想在服务器启动时运行该命令并保持其运行,无论发生什么,都应该了解Laravel是如何执行队列工作者的。这会让你知道如何做到这一点。

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

有了Supervisor,您可以随时启动和运行任何命令。如果命令崩溃,它实际上能够重新启动命令。

php过滤到特定名称时,您可以使用pgrep -f php获得PID。例如

pgrep -f start:parser

最新更新