【问题标题】:shell script in PHP/HTMLPHP/HTML 中的 shell 脚本
【发布时间】:2021-04-18 00:12:57
【问题描述】:

我有一个 shell 脚本,可以从 ps aux grep python3

shell 脚本 (pythoncheck.sh)

if ps aux | grep -v "grep" | grep "python3" ; then echo "found" ; fi

如何在 html 或 php 中调用此脚本,当单击按钮时,如果结果“找到”状态变为绿色,如果为 null,则状态变为红色

这可以用 PHP 实现吗?

【问题讨论】:

  • @feverdream 完全错误

标签: python php html shell


【解决方案1】:

查看 PHP 中的 shell_exec 函数。

一种方法是创建一个运行脚本然后回显结果的 PHP 文件,然后您可以在单击按钮时向该文件发出网络请求,并使用响应来更改您的状态颜色。

py-check.php

echo shell_exec('if ps aux | grep -v "grep" | grep "python3" ; then echo "found" ; fi
')

index.html

<button onclick="pyCheck">Check Python Status</button>

<script>
  function pyCheck() {
    fetch('/py-check.php')
      .then(function(response) {
        return response.text();
      })
      .then(function(text) {
        if (text === found) {
          // Set status green here ✅
        } else {
          // Set status red here ?
        }
      })
      .catch(function(error) {
        console.error('Request failed', error)
      });
  }
</script>

【讨论】:

    猜你喜欢
    • 2015-12-01
    • 2012-06-29
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 2021-12-06
    • 2013-06-13
    • 1970-01-01
    • 2012-01-15
    相关资源
    最近更新 更多