简体   繁体   English

在 bash 和 php 之间共享数据

[英]Share data between bash and php

We are trying to create a project that will run on linux.我们正在尝试创建一个将在 linux 上运行的项目。 But we want to see results from browser.但是我们想从浏览器中看到结果。 We want to use PHP for that but we are not really sure how to share data between those two environment.我们想为此使用 PHP,但我们不确定如何在这两个环境之间共享数据。 We dont want to use MySql or any other dbms for that not to use ram just for 1 or 2 data.我们不想使用 MySql 或任何其他 dbms 来避免仅将 ram 用于 1 或 2 个数据。

So the question is;所以问题是; "We want to share 1 or at most 2 data between bash and PHP. How can we do this without third party application or server?" “我们希望在 bash 和 PHP 之间共享 1 个或最多 2 个数据。如果没有第三方应用程序或服务器,我们如何才能做到这一点?”

Thanks for answers Baris谢谢巴里斯的回答

I assume you have a bash script and you want to run that from PHP and handle the output in some way.我假设您有一个 bash 脚本,并且您想从 PHP 运行它并以某种方式处理 output。 PHP has several functions to accomodate that. PHP 具有多种功能来适应这种情况。

The backtick operator example in the PHP docs is: PHP 文档中的反引号运算符示例是:

<?php
$output = `ls -al`;
echo "<pre>$output</pre>";
?>

It's not entirely clear from your question what you're trying to do...but one possible solution would be to write your data to a file or two.您的问题并不完全清楚您要做什么......但一种可能的解决方案是将您的数据写入一个或两个文件。 Either PHP or your cli environment can read/write the file to get/set the data. PHP 或您的 cli 环境都可以读取/写入文件以获取/设置数据。 Note that if both environments expect to write the data you would need to implement some sort of locking mechanism.请注意,如果两个环境都希望写入数据,则您需要实现某种锁定机制。

You could also use something like memcached , which would provide you with a memory-based key/value store without the "overhead" of a complete RDBMS solution.您还可以使用memcached之类的东西,它可以为您提供基于内存的键/值存储,而无需完整 RDBMS 解决方案的“开销”。

This is really a Stack Exchange question, but PHP runs fine in CLI mode and can do whatever bash would do, without much extra effort.这确实是一个 Stack Exchange 问题,但是 PHP 在 CLI 模式下运行良好,并且可以执行 bash 可以执行的任何操作,而无需付出太多额外的努力。 So I would use PHP for the background or CLI process (optionally calling a bash shell script as needed if you want to use PHP as a wrapper only) then use PHP also for the web part. So I would use PHP for the background or CLI process (optionally calling a bash shell script as needed if you want to use PHP as a wrapper only) then use PHP also for the web part. The two parts can share PHP code if required, particularly the part that reads/writes from a shared file.如果需要,这两个部分可以共享 PHP 代码,特别是从共享文件读取/写入的部分。

Locking of this file will be the main challenge as mentioned - creating a lock directory with mkdir (since it's atomic) would be one way, but you'd also need to ensure locks can be cleaned up.如前所述,锁定此文件将是主要挑战 - 使用 mkdir 创建锁定目录(因为它是原子的)将是一种方法,但您还需要确保可以清除锁定。

UPDATE: this approach would also work if you want to write to shared RAM instead of a file - you could use memcached or similar from PHP, but there is no way to do that from bash.更新:如果您想写入共享 RAM 而不是文件,这种方法也可以工作 - 您可以使用 PHP 中的 memcached 或类似内容,但 bash 没有办法做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM