【问题标题】:Execute an installed software command on a remote linux server using a PHP page使用 PHP 页面在远程 linux 服务器上执行已安装的软件命令
【发布时间】:2020-03-05 12:03:03
【问题描述】:

我正在开发一个 3D 打印网页。为了计算材料和时间,我试图让网络在文件上运行 cura-engine 软件(控制台程序)。

我在 Google Cloud Platform 中创建了一个虚拟机,在 debian 10 下运行 LAMP 堆栈。我使用 apt 安装了 cura'engine 包,我可以从终端(通过 ssh)运行它:

eduardoj_vieirav@instance-1-vm:/var/www/html$ CuraEngine

Cura_SteamEngine version 3.3.0
Copyright (C) 2018 Ultimaker

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
...

但是当尝试使用 exec 或 shell_exec 使用 php 执行此命令时,我没有输出。

<?php
echo exec('CuraEngine');
?>

我也尝试过对文件执行操作,我可以再次使用以下命令从 ssh 执行此操作:

eduardoj_vieirav@instance-1-vm:/var/www/html$ CuraEngine slice -v -j resources/definitions/creality_ender3.def.json -o gcode/output.gcode -l STL/cube.stl

但它又不能从 php 运行

<?php
echo exec('CuraEngine slice -v -j resources/definitions/creality_ender3.def.json -o gcode/output.gcode -l STL/cube.stl');
?>

我觉得奇怪的是我无法执行 CuraEngine,因为其他命令(例如 uname -a0)会返回输出。

【问题讨论】:

  • 我正在尝试做和你一样的事情。我尝试过的事情:从 sh 文件运行 CuraEngine;并将终端输出重定向到带有CuraEngine -options 2&gt; cura_output 的文件。到目前为止没有运气。使用 shell_exec 调用时,CuraEngine 不运行。你设法解决了吗?谢谢

标签: php linux server console


【解决方案1】:

您可能对 PHP 中的 $PATH 环境变量有疑问。尝试指定CuraEngine 的绝对路径(如果需要,使用whereis CuraEngine 命令)。

【讨论】:

  • 感谢您的回复柳德米拉。 CuraEngine 本地安装在/usr/bin/CuraEngine 的虚拟机上。使用绝对路径我也得到相同的结果。
  • 正如我从手册中看到的,您的程序 Cura_SteamEngine 需要终端 (tty)。但是当您使用 exec/shell_exec 运行程序时,虚拟终端不存在!您可以尝试使用 Cura 的 --connect 选项使其与用于控制的套接字而不是 tty 设备一起使用。
  • 我认为柳德米拉的方向是正确的。谢谢你。如果我找到解决方案,我会返回。
【解决方案2】:

这可能都是由于权限,因为我可以从许多其他在线帖子中找到。 尝试这样写(注意末尾的 2>&1),输出应该会指出是否是权限错误。

<?php
echo exec('CuraEngine slice -v -j resources/definitions/creality_ender3.def.json -o gcode/output.gcode -l STL/cube.stl  2>&1');
?>

之后,如果是权限错误,请应用this 帖子中提到的所有权更改,它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多