0

这是我的程序导入子流程

print "Content-type:text/html\r\n\r\n"
print "File starting to execute"
print "<br>"
proc = subprocess.Popen(["sudo", "python", "test3.py"], stdout=subprocess.PIPE)
output = proc.stdout.read()
print "output is %s" %output

print "<br>"
print "File Executed Awesomely"

因此,当我从命令行运行它时,它运行良好,如下所示->

[root@localhost html]# python test2.py
Content-type:text/html


File starting to execute
<br>

output is .
Sent 1 packets.

<br>
File Executed Awesomely
[root@localhost html]# 

那就是完美的“。发送了 1 个数据包”。是我想要的。但是当我从网页运行它时,网页只有

File starting to execute
output is
File Executed Awesomely

所以我最初认为这是因为我在抓取输出时做错了,但我用wireshark在端口上监听(它调用的我的另一个程序发送一个数据包)并且看起来没有数据包通过网页调用显示,但它确实在我在命令行上调用它(以相同的方式)。查看我的 apache error_log->

[Wed Jan 18 18:15:11 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Jan 18 18:15:11 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Jan 18 18:15:11 2012] [notice] Digest: done
[Wed Jan 18 18:15:11 2012] [warn] ./mod_dnssd.c: No services found to register
[Wed Jan 18 18:15:11 2012] [notice] Apache/2.2.17 (Unix) DAV/2 configured -- resuming normal operations

关于如何修复它的任何建议,以便我的 apache cgi-bin 脚本以与命令行相同的方式运行?

编辑:在几次调用后查看日志,它会重复执行此操作

[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] :
[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] sorry, you must have a tty to run sudo 
4

1 回答 1

1

消息sorry, you must have a tty to run sudo是关键。首先,让你的 apache 运行 sudo 至少可以说是危险的,但如果你真的想这样做......有一种方法,编辑/etc/sudoers( visudo) 并找到Defaults requiretty部分 ( man sudoers)。

注意:永远不要让 apache 使用 sudo 运行任何东西,准确地指定它需要做什么,仅此而已!

顺便说一句:如果您启用了 SELinux 或其他 LSM 模块,它可能仍然无法工作。

于 2012-01-18T23:32:08.120 回答