简体   繁体   English

在 lambda 中运行 os.system 模块时遇到“sh: php: command not found”错误

[英]encountered “sh: php: command not found” error while running os.system module in lambda

I'm trying to run a php script in aws lambda using python runtime.我正在尝试使用 python 运行时在 aws lambda 中运行 php 脚本。 I tried using python os.system module to call the php page to execute the script.我尝试使用 python os.system 模块调用 php 页面来执行脚本。 The code is given below代码如下

import os
def lambda_handler(event, context):
    os.system("php /home/admin/web/####/###.php")

On running the above code, it displays error: "sh: php: command not found".运行上述代码时,显示错误:“sh: php: command not found”。 I do not have much knowledge either in python scripting or shell scripting.我对 python 脚本或 shell 脚本没有太多了解。 So any help would be much appreciated.所以任何帮助将不胜感激。

Update: New code更新:新代码

def lambda_handler(event, context):
    stream = os.popen("/usr/bin/php /home/admin/web/path/")
    output = stream.read()
    print(output) 

Used the new code but its displaying this error now: /bin/sh: /usr/bin/php74: No such file or directory.使用了新代码,但现在显示此错误:/bin/sh: /usr/bin/php74: No such file or directory。

I don't understand, because php is installed into the system, but yet it is telling no such file exists.我不明白,因为 php 已安装到系统中,但它却告诉不存在这样的文件。

You can not run PHP scripts in the default Python Lambda runtime.不能在默认的Python Lambda 运行时中运行 PHP 脚本。 There is no PHP installed.没有安装 PHP。

So you have three options:所以你有三个选择:

  1. Migrate your PHP code to Python.将您的 PHP 代码迁移到 Python。
  2. Create a Docker image with Python and PHP and use it as Lambda runtime.使用 Python 和 PHP 创建 Docker 映像并将其用作 Z04A7DA3C5B04BCAD85DA1EEBBB923 运行时。 This feature was added recently, you can read about it this blog post .此功能是最近添加的,您可以阅读这篇博文
  3. Create a custom Lambda runtime, as described in the documentation .文档中所述,创建自定义 Lambda 运行时。

Your original question contained the PHP script, that you want to run.您的原始问题包含您要运行的 PHP 脚本。 It only contained a very trivial MySQL query.它只包含一个非常简单的 MySQL 查询。 It should be very easy to migrate that one line of PHP code to Python.将一行 PHP 代码迁移到 Python 应该非常容易。

The PHP code: PHP 代码:

<?php
$con1 = mysqli_connect("endpoint","###","###","database");
if(mysqli_query($con1,"insert into ex_inbox(time) values ('Done')") or die("the eror ".mysqli_error($con1)));
?>

Therefore, option #1 is the one I'd recommend.因此,选项#1 是我推荐的选项。 You can read how to do insert into a MySQL database using Python in this guide .您可以在本指南中阅读如何使用 Python 插入 MySQL 数据库。

Please specify the full path!!!请注明完整路径!!!

for example:例如:

The actual PHP path should be based on your own实际的PHP路径要根据自己

/####/####/php/72/bin/php /home/admin/web/####/###.php

My Python code我的 Python 代码

import os


def main():

    stream = os.popen("C:/Users/Administrator/Desktop/php/xxx/xxx/php/php7.2.9nts/php  C:/xxx/xxx/WWW/test/test1.php")
    output = stream.read()
    print(output) # Successful and effective execution

if __name__ == '__main__':
    main()

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

相关问题 Python 运行 os.system(“kinit”) 时出错 - sh: 1: kinit: not found - Python error when running os.system(“kinit”) - sh: 1: kinit: not found 通过cygwin运行时找不到Python os.system“启动”命令 - Python os.system “start” command not found while running through cygwin 当 os.system(command) 正在运行时显示 tkinter 窗口 - while os.system(command) is running display tkinter window python os.system 问题:“sh:1:[command] not found”; 命令以交互方式工作 - python os.system issue: "sh: 1: [command] not found"; command works interactively 运行os.system virtualenv OpenCV时导入模块错误 - Import module error when running os.system virtualenv OpenCV 传递给os.system()的Shell命令失败:sh:2:语法错误:“ |”意外 - Shell command passed to os.system() fails: sh: 2: Syntax error: “|” unexpected 使用os.system(A)运行命令会产生不同的输出,然后在shell中运行A - Running a command with os.system(A) produces different output then running A in shell 使用python在os.system的参数中使用一行“未找到命令” - “command not found” using line in argument to os.system using python 字符串格式化后找不到os.system命令 - os.system command not found after string formatting 找不到python os.system() - Python os.system() not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM