[英]trying to run a python script from a php script: returns a pid but doesn't execute
I am trying to execute a python script in the background and get a pid from a php script which is called by a button click. 我试图在后台执行python脚本,并从通过单击按钮调用的php脚本获取pid。 The issue here is that the PID doesn't exist and the task of the python script is not done. 这里的问题是PID不存在,并且python脚本的任务没有完成。 Im not sure what is happening. 我不确定发生了什么。 I have tested the python script using terminal, it works like a charm. 我已经使用终端测试了python脚本,它的工作原理很吸引人。
<!DOCTYPE html>
<html>
<body>
<?php
echo 'Starting the mapping process';
echo exec('python frist_script.py > /dev/null 2>&1 & echo $!');
//echo exec('ls > /dev/null 2>&1 & echo $!;');
?>
</body>
</html>
The python script just executes a ls
and writes the output to a file. python脚本仅执行ls
并将输出写入文件。 The code for the python script as follows. python脚本的代码如下。
import os
import datetime, time
import sys
import argparse
import psutil
import glob
import select
os.system('ls >output_files.dat')
Note: This is not the final script, but a test script to confirm that my python script call works fine. 注意:这不是最终脚本,而是一个测试脚本,用于确认我的python脚本调用正常运行。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
Thanks 谢谢
PS: Output of ls -al
PS: ls -al
输出
-rw-rw-r-- 1 acs www-data 262 May 18 16:31 execute.php
-rwxr-xr-x 1 www-data www-data 147 May 18 16:17 frist_script.py
I have also tried shell_exec(); 我也尝试过shell_exec();
EDIT: I tried to run a simple shell script, but that doesn't get executed as well but Shell commands work. 编辑:我试图运行一个简单的shell脚本,但不会得到执行,但Shell命令可以正常工作。
UPDATE: The python script or the shell script works when executed from terminal php execute.php
but doesn't work when running from the web-server. 更新:从终端php execute.php
执行时python脚本或shell脚本有效,但从Web服务器运行时不起作用。
Question : ... but doesn't work when running from the web-server. 问题 :...但是从Web服务器运行时不起作用。
Does your Server have PHP support? 请问您的服务器有 PHP的支持?
Create a new script or replace your os.system(...
with this: 创建一个新脚本或将os.system(...
替换为:
import subprocess
try:
# os.system('ls >output_files.dat')
lsoutput = subprocess.check_output('ls', stderr=subprocess.STDOUT)
print('ls output:', lsoutput)
except subprocess.CalledProcessError as e:
print("CalledProcessError:", e)
Call this script from your php script
. 从您的php script
调用此php script
。
Change from 从
echo exec('python frist_script.py > /dev/null 2>&1 & echo $!');
to 至
echo exec('python frist_script.py >> /tmp/php_exec 2>&1');
to see any error message. 查看任何错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.