简体   繁体   English

如果我有一个 Python 脚本,我从 web 服务器开始在 PHP 中,我该如何停止它?

[英]If I have a Python script I started in PHP from a web server, how do I stop it?

I have an Apache Web server on my RaspberryPi 4 that has a program written in Python on it that will turn on an 8x8 matrix of red led's.我的 RaspberryPi 4 上有一个 Apache Web 服务器,上面有一个用 Python 编写的程序,它将打开一个 8x8 矩阵。 I have a HTML and PHP page on the web server so I can remotely execute that script.我在 web 服务器上有一个 HTML 和 PHP 页面,因此我可以远程执行该脚本。 The HTML is just 2 buttons and on the press of the button it goes to this PHP program: HTML 仅有 2 个按钮,按下按钮即可进入此 PHP 程序:

<?php

  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
  error_reporting(E_ALL);

  if ($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['on']))
  {
    lightOn();
  } elseif ($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['off']))
  {
    lightOff();
  }

  function lightOn()
  {
    $command = escapeshellcmd('sudo python3 led_display.py');
    $output = shell_exec($command);

    header('Location: index.html');
    exit;

  }

  function lightOff()
  {
    $command = escapeshellcmd('');
    $output = shell_exec($command);

    header('Location: index.html');
    exit;
  }

?>

This PHP program just recognizes which of the 2 buttons was pressed and on the press executes a function.此 PHP 程序仅识别按下了 2 个按钮中的哪一个,并在按下时执行 function。 For the lightOn() function it is simple as I just execute the script that drives the 8x8 led matrix.对于 lightOn() function 这很简单,因为我只需执行驱动 8x8 LED 矩阵的脚本。 However I want the user to be able to stop the script at any time by pressing the other button.但是我希望用户能够通过按下另一个按钮随时停止脚本。 On a normal command line interface I would type 'Control + c' to stop any running script.在普通的命令行界面上,我会键入“Control + c”来停止任何正在运行的脚本。 How would I achieve that in PHP through shell_exec, or any other method?我将如何通过 shell_exec 或任何其他方法在 PHP 中实现这一点?

In your server, find out what's the process id (PID) via ps , top or even htop commands.在您的服务器中,通过pstop甚至htop命令找出进程 ID (PID) 是什么。 Than you can kill it with the command kill {pid_number} .比你可以用命令kill {pid_number}杀死它。

Something like:就像是:

$ ps aux | grep led_display # the first number that appears is the PID. let's assume it's 1234
$ kill 1234

Assuming that you have control over the sudoers file and know how to make it so your web-server process can execute the command referenced in lightOff without a password you should be able to do假设您可以控制sudoers文件并知道如何制作它,以便您的网络服务器进程可以执行lightOff中引用的命令,而无需密码,您应该能够做到

$command = escapeshellcmd('sudo pkill -TERM led_display.py');

Of course that would terminate ANY process called led_display.py , not just the one you started from the web-server process.当然,这会终止任何名为led_display.py的进程,而不仅仅是您从 Web 服务器进程启动的进程。

EDIT:编辑:

What happens if you background the process?如果你在后台处理这个过程会发生什么?

$command = escapeshellcmd('sudo python3 led_display.py > /dev/null 2>&1 &');

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

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