简体   繁体   English

从php运行Python脚本,保存进程pid,不要等待它完成

[英]Run Python script from php, save process pid, and don't wait it to finish

I have a PHP script that runs a python script with shell_exec('myScript.py'). 我有一个PHP脚本,该脚本使用shell_exec('myScript.py')运行python脚本。 It works nicely, but I don't want to wait that python script to finish. 它工作得很好,但我不想等待该python脚本完成。 Also I need to know that process pID so I can stop it if I want. 另外,我需要知道该进程的pID,以便在需要时可以将其停止。 How can I do both things? 我该怎么办? I know that adding "& $!" 我知道添加“&$!”。 in the terminal after "myScript.py" gives me the pID, and that I can kill that process with: "kill -SIGTERM pID", but only from terminal. 在终端“ myScript.py”之后给我pID,然后我可以使用“ kill -SIGTERM pID”杀死该进程,但只能从终端执行。 Thanks in advance. 提前致谢。

In more detail, this is what i want to achieve: I got a raspberry pi. 更详细地说,这就是我要实现的目标:我得到了一个树莓派。 That raspberry pi can control stuff with python scripts (lights, sockets...). 那个树莓派可以用python脚本(灯光,套接字...)来控制东西。 Also, i got a home webserver installed in that raspberry pi. 另外,我在该树莓派中安装了一个家庭网络服务器。 The objective is having a webpage, and being able to run and stop that python scripts from it. 目的是拥有一个网页,并能够从中运行和停止该python脚本。

you gotta put some bash magic into it. 您必须在其中添加一些bash魔术。

 $a = exec('python myScript.py > /dev/null 2>&1 & echo $!');
 echo $a;

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

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