[英]Laravel - Linux(ubuntu) Real-time process and show output on webpage(blade)
I want to get the real-time output of a running process on my server.我想在我的服务器上获得正在运行的进程的实时输出。 For the script I made something simple in bash:
对于脚本,我在 bash 中做了一些简单的事情:
Script 1 (script.sh).脚本 1 (script.sh)。
#!/bin/bash
y=0;
while [ $y > 0 ]; do
y=$((y+1))
sleep 1;
echo $y
echo "I am baking pies. I have $y so far."
done
Script 2 (test.sh).脚本 2 (test.sh)。
#!/bin/bash
~/script.sh > /dev/null &
pid=$!
echo $pid
strace -e trace=write -s1000 -fp $pid 2>&1 \
| grep --line-buffered -o '".\+[^"]"' \
| grep --line-buffered -o '[^"]\+[^"]' \
| while read -r line; do
printf "%b " $line;
done
The second script's role is to run the first one, get it's pid and than trace the output of that pid.第二个脚本的作用是运行第一个脚本,获取它的 pid,然后跟踪该 pid 的输出。 On the server side this should be enough but I may be wrong.
在服务器端,这应该足够了,但我可能错了。 However, on the laravel side I couldn't figure a way to get the live output of that script.
但是,在 Laravel 方面,我无法找到获取该脚本实时输出的方法。
Laravel, using Symfony (using the Symfony Docs example: https://symfony.com/doc/current/components/process.html ). Laravel,使用 Symfony(使用 Symfony 文档示例: https ://symfony.com/doc/current/components/process.html)。
$process = new Process(['/test.sh']);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
The script works almost like it should be, but there is a problem:该脚本几乎像它应该的那样工作,但有一个问题:
When the script runs laravel only prints the pid and doesn't wait for the output of the strace command.当脚本运行时,laravel 只打印 pid 而不会等待 strace 命令的输出。 Now, is there a way to get the live output of the script while it is running?
现在,有没有办法在脚本运行时获取脚本的实时输出? If it is not possible using Laravel and Symfony alone could I achieve it by using VueJS (which I am familiarized with and practiced in the past few months)?
如果单独使用 Laravel 和 Symfony 是不可能的,我是否可以通过使用 VueJS(我在过去几个月中熟悉并实践过)来实现它?
yes u can do like that是的,你可以这样做
for example i created one script test.sh
and store in laravel root project ..例如,我创建了一个脚本
test.sh
并存储在laravel 根项目中..
a=0
while [ $a -lt 10 ]
do
echo $a
sleep 1
a=`expr $a + 1`
done
if you are ubuntu user then give a permission to execute a script file permision like that
如果您是 ubuntu 用户,则授予执行这样的脚本文件权限的权限
sudo chmod +x test.sh
now in laravel create new console command
现在在 laravel 中创建
new console command
php artisan make:command test
php artisan make:命令测试
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
class test extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'test description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$process = new Process([base_path('test.sh')]);
$process->start();
foreach ($process as $type => $data) {
if ($process::OUT === $type) {
info($data); //output store in log file..
$this->info($data); //show output in console..
// $this->info(print_r($data,true)) // if output is array or object then used
} else {
$this->warn("error :- ".$data);
}
}
$this->info("get output");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.