douzhenzu0247 2015-08-26 02:02
浏览 495
已采纳

在后台执行Laravel / Symfony / Artisan命令

I need to execute a Laravel long running process in the background for consuming the Twitter Streaming API. Effectively the php artisan CLI command I need to run is

nohup php artisan startStreaming > /dev/null 2>&1 &

If I run that myself in the command line it works perfectly.

The idea is that I can click a button on the website which kicks off the stream by executing the long running artisan command which starts streaming (needs to run in the background because the Twitter Streaming connection is never ending). Going via the command line works fine.

Calling the command programatically however doesn't work. I've tried calling it silently via callSilent() from another command as well as trying to use Symfony\Component\Process\Process to run the artisan command or run a shell script which runs the above command but I can't figure it out.

Update If I queue the command which opens the stream connection, it results in a Process Timeout for the queue worker

I effectively need a way to run the above command from a PHP class/script but where the PHP script does not wait for the completion/output of that command.

Help much appreciated

  • 写回答

2条回答 默认 最新

  • dongshenghe1833 2015-08-26 02:12
    关注

    The Symfony Process Component by default will execute the supplied command within the current working directory, getcwd().

    The value returned by getcwd() will not be the Laravel install directory (the directory that contains artisan), so the command will most likely returning a artisan: command not found message.

    It isn't mentioned in the Process Component documentation but if you take a look at the class file, we can see that the construct allows us to provide a directory as the second parameter.

    public function __construct(
        $commandline, 
        $cwd = null, 
        array $env = null, 
        $input = null, 
        $timeout = 60, array 
        $options = array())
    

    You could execute your desired command asynchronously by supplying the second parameter when you initialise the class:

    use Symphony\Component\Process\Process;
    
    $process = new Process('php artisan startStreaming > /dev/null 2>&1 &', 'path/to/artisan'); 
    $process->start();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件