adnanhussainturki / unix-screen-php
仅为PHP中的Unix screen的一个包装器
0.15
2023-02-05 08:32 UTC
Requires
- illuminate/database: >=8.82
- illuminate/events: >=8.82
- nahid/jsonq: ^6.0
- symfony/process: >=5.4
README
unix-screen-php
在PHP脚本中运行shell命令或脚本的正确方式。
该包专门设计用于与Laravel框架一起使用,但也可用于Core PHP,同时配合Laravel的Eloquent
教程播放列表
点击在YouTube上观看播放列表
安装
可以通过执行以下命令使用Composer轻松安装此包:
composer require adnanhussainturki/unix-screen-php
先决条件
- 使用命令
php artisan make:model Process
创建模型名称Process
- 使用命令
php artisan make:migration create_process_table
创建一个新的迁移 - 迁移中使用的代码如下
Schema::create('processes', function (Blueprint $table) { $table->id(); $table->string("slug")->unique(); $table->integer("timeout")->default(30); $table->integer("exitcode")->nullable(); $table->json("data"); $table->boolean("closed")->default(false); $table->boolean("success")->nullable(); $table->dateTime("started_at"); $table->bigInteger("started_at_unix"); $table->text("remark")->nullable(); $table->timestamps(); });
执行模式
该包提供两种命令或脚本的执行模式
-
同步(不推荐)适合运行时间短的命令,如
ls
或uname
等。运行命令或shell脚本,等待执行完成。此方法不推荐,因为它可能会引发PHP超时错误或Web服务器超时错误。此外,此方法对用户体验也不太好。 -
异步(推荐)非常适合运行耗时命令或脚本,如
top
或cp
或mv
等。
创建Screen实例
<?php
use myPHPnotes\Screen;
$screen = new Screen(storage_path("screen"), new \App\Models\Process());
异步运行命令
<?php
use myPHPnotes\Screen;
$screen = new Screen(storage_path("screen"), new \App\Models\Process());
$command = "cp -r /etc/ ~/etc/";
$timeout = 30;
$arguments = [];
$identifier = "myFirstAsynchronouslyRunningCommand"; # Can be null, to use the autogenerated
$screen->executeCommand($request->command, $arguments, $identifier, $timeout);
同步运行命令
<?php
use myPHPnotes\Screen;
$screen = new Screen(storage_path("screen"), new \App\Models\Process());
$command = "cp -r /etc/ ~/etc/";
$timeout = 30;
$arguments = [];
$identifier = "myFirstSynchronouslyRunningCommand"; # Can be null, to use the autogenerated
$screen->executeCommandNow($request->command, $arguments, $identifier, $timeout);
异步运行shell脚本
<?php
use myPHPnotes\Screen;
$screen = new Screen(storage_path("screen"), new \App\Models\Process());
$script = "
echo "Welcome" $1
useradd john -m
exitcode=$? // Needs to be set to define the exit code for the whole script
touch ~/john/happy
";
// Save the script to temporary file
$filename = "shell_script_".time()."_".md5(random_bytes(1));
$temp_file_path = storage_path("screen/temp/{$filename}.sh");
file_put_contents($temp_file_path, $script);
$timeout = 30;
$arguments = ["Admin"];
$identifier = "myFirstAsynchronouslyRunningScript"; # Can be null, to use the autogenerated
$screen->executeFile($temp_file_path, $arguments, $identifier, $timeout);
异步运行shell脚本
<?php
use myPHPnotes\Screen;
$screen = new Screen(storage_path("screen"), new \App\Models\Process());
$script = "
echo "Welcome" $1
useradd john -m
exitcode=$? // Needs to be set to define the exit code for the whole script
touch ~/john/happy
";
// Save the script to temporary file
$filename = "shell_script_".time()."_".md5(random_bytes(1));
$temp_file_path = storage_path("screen/temp/{$filename}.sh");
file_put_contents($temp_file_path, $script);
$timeout = 30;
$arguments = ["Admin"];
$identifier = "myFirstSynchronouslyRunningScript"; # Can be null, to use the autogenerated
$screen->executeFileNow($temp_file_path, $arguments, $identifier, $timeout);
设置心跳
必须每分钟(或更少)调用一次Screen
对象的heartbeat()
函数。此heartbeat()
调用将检查并更新运行中的进程的退出码和超时。
<?php
$screen = new Screen(storage_path("screen"), new \App\Models\Process());
$screen->heartbeat();
您可以在Laravel命令中使用以下代码
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Helpers\Screen;
class Heartbeat extends Command
{
protected $signature = 'screen:heartbeat';
protected $description = 'Command to check and update the status of the processes';
public function __construct()
{
parent::__construct();
}
public function handle()
{
for ($i=0; $i < 5; $i++) {
Screen::get()->heartbeat();
sleep(1);
}
return 0;
}
}
Kernel.php
$schedule->command('screen:heartbeat')->everyMinute();
请确保您的Laravel任务调度正确设置。
买我一杯咖啡
如何贡献
-
创建分支,进行更改,并发送拉取请求。
-
提出问题
许可证
许可协议:MIT。