简体   繁体   中英

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'). It works nicely, but I don't want to wait that python script to finish. Also I need to know that process pID so I can stop it if I want. 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. 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...). 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.

you gotta put some bash magic into it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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