exec 未找到文件

发布于 2024-10-08 04:27:44 字数 394 浏览 0 评论 0原文

我在使用 exec php 函数时遇到问题。 当我这样做时:

ls -al /dev/sdf1  

这没有找到存档(ls:无法访问/dev/sdf1:没有这样的文件或目录),但是如果我运行此命令,在控制台中会显示信息。 我能做些什么? 即使以 root 身份运行 php 脚本也会发生这种情况!

$mountcommand = "ls -al /dev/$unit  2>&1";
exec("$mountcommand", &$mountoutput, &$mountresult);
print_r($mountoutput);
echo "\n$mountcommand\n\n";

这是一个分叉的过程

I'm having problems with the exec php function.
When I do:

ls -al /dev/sdf1  

This doesn't found the archive (ls: cannot access /dev/sdf1: No such file or directory), but if I run this command, in console that show me the information.
What can I do?
That happen even running the php script as a root!!

$mountcommand = "ls -al /dev/$unit  2>&1";
exec("$mountcommand", &$mountoutput, &$mountresult);
print_r($mountoutput);
echo "\n$mountcommand\n\n";

This is in a forked process

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-10-15 04:27:44

对我有用。

磁盘:

frayser@gentoo ~/doc/Answers/src/PHP $ disk

sda     111.790 GB
sdb     233.762 GB
sdc     233.762 GB
sdd     233.762 GB
sde     279.481 GB
------------------
Total:  1092.558 GB

使用 hde:

frayser@gentoo ~/doc/Answers/src/PHP $ l /dev/sde
brw-rw---- 1 root disk 8, 64 Dec  2 13:36 /dev/sde

使用 PHP:

frayser@gentoo ~/doc/Answers/src/PHP $ php lsdev.php 
Z: brw-rw---- 1 root disk 8, 64 Dec  2 13:36 /dev/sde

脚本:

frayser@gentoo ~/doc/Answers/src/PHP $ cat lsdev.php 
<?php
$z=exec("ls -lr /dev/sde");
print "Z: " . $z . "\n";
?>

更新

它也适用于 ls -al
请出示你的脚本。

更新 2

按照 ircmaxell 的 chroot 建议:假设 /dev(如 PHP 进程中所示)是真实的 chroot 传真的特殊版本。假设 /dev/sdf1 是可移动设备。当插入sdf1(媒体)时系统会自动在真实的/dev中创建设备;但chroot版本没有更新。因此,要检测这种情况,请从 PHP 和命令行中查找 /dev 之间的差异。

一项测试是 /dev 上的 ls -id(来自 PHP 和命令行)。这会打印 /dev 的 inode。 inode 编号是否不匹配?

/dev/sdf1 可以删除吗?装了吗?如果已安装; PHP 进程是否从 PHP 中看到已安装的文件系统:ls $mount_point

Stack Overflow 上和其他地方还列出了其他 chroot 测试;但我还没有发现很多好的方法:如果 chroot 正确完成,则很难检测到。

更新 3

这可能是正在发生的情况:设备 (/dev/sdf1) 在创建后需要一段时间才能出现;因此有必要在创建设备和尝试挂载之间暂停:

之前:设备已创建;但它不在那里...

Array
(
    [0] => Logging out of session [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [1] => Logout of [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [4] => ls: cannot access /dev/sdg: No such file or directory
)

{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; ls -al /dev/sdg ;} 2>&1

之后:添加了 1 秒暂停,并且设备 (sdg) 可用...

Array
(
    [0] => Logging out of session [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [1] => Logout of [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [4] => brw-rw---- 1 root disk 8, 96 Dec 18 05:27 /dev/sdg
)

{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; sleep 1; ls -al /dev/sdg ;} 2>&1

带有暂停的 PHP ...

<pre>
<?php
$unit='sdg';
$reset="test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u";

$connect="sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l";

$test="ls -al /dev/$unit";

$mountcommand = "{ $reset; $connect; sleep 1; $test ;} 2>&1";
exec("$mountcommand", $mountoutput, $mountresult);
print_r($mountoutput); 
echo "\n$mountcommand\n\n";
?>
</pre>

/etc/sudoers 是配置与上一个关于从 PHP 挂载失败的问题中报告的相同。

因此,在 iscsiadm 创建设备后添加一两秒的 sleep() 。睡眠可以在 PHP 中完成,而不是在 shell 的 exec() 中完成。

Works for me.

The disks:

frayser@gentoo ~/doc/Answers/src/PHP $ disk

sda     111.790 GB
sdb     233.762 GB
sdc     233.762 GB
sdd     233.762 GB
sde     279.481 GB
------------------
Total:  1092.558 GB

Using hde:

frayser@gentoo ~/doc/Answers/src/PHP $ l /dev/sde
brw-rw---- 1 root disk 8, 64 Dec  2 13:36 /dev/sde

Using PHP:

frayser@gentoo ~/doc/Answers/src/PHP $ php lsdev.php 
Z: brw-rw---- 1 root disk 8, 64 Dec  2 13:36 /dev/sde

The script:

frayser@gentoo ~/doc/Answers/src/PHP $ cat lsdev.php 
<?php
$z=exec("ls -lr /dev/sde");
print "Z: " . $z . "\n";
?>

Update

It also works with ls -al.
Please show your script.

Update 2

Along the lines of the chroot suggestion by ircmaxell: Supposing that /dev, as seen in the PHP process, was a special chroot facsimile of the real one. And supposing that /dev/sdf1 is a removable device. The when sdf1(the media) is inserted the system automatically creates the device in the real /dev; but the chroot version isn't updated. So, to detect this situation look for differences between /dev as seen from PHP and from the commandline.

One test is ls -id on /dev(from PHP and the commandline). This prints the inode of /dev. Is there a mismatch in inode numbers?

Is /dev/sdf1 removable? Is it mounted? If it is mounted; does the PHP process see the mounted filesystem: ls $mount_point from PHP it.

There are other chroot tests listed on Stack Overflow and elsewhere; but I haven found many good ones: If a chroot is done correctly, it is hard to detect.

Update 3

This may be what is happening: The device (/dev/sdf1) takes a while to appear after it is created; so it is necessary to pause between creating the device and attempting to mount it:

Before: The device is created; but it isn't there...

Array
(
    [0] => Logging out of session [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [1] => Logout of [sid: 4, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [4] => ls: cannot access /dev/sdg: No such file or directory
)

{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; ls -al /dev/sdg ;} 2>&1

After: Added a 1-second pause, and the device (sdg) is available...

Array
(
    [0] => Logging out of session [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [1] => Logout of [sid: 5, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [2] => Logging in to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]
    [3] => Login to [iface: iface0, target: iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e, portal: 172.16.0.1,3260]: successful
    [4] => brw-rw---- 1 root disk 8, 96 Dec 18 05:27 /dev/sdg
)

{ test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u; sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l; sleep 1; ls -al /dev/sdg ;} 2>&1

The PHP with the pause ...

<pre>
<?php
$unit='sdg';
$reset="test -b /dev/sdg && sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -u";

$connect="sudo iscsiadm -m node -T iqn.2004-08.org.frayser:gen2-dummy.ad4s0.dos.e -l";

$test="ls -al /dev/$unit";

$mountcommand = "{ $reset; $connect; sleep 1; $test ;} 2>&1";
exec("$mountcommand", $mountoutput, $mountresult);
print_r($mountoutput); 
echo "\n$mountcommand\n\n";
?>
</pre>

The /etc/sudoers is configured the same as reported in the previous question about mount failing from PHP.

So add a second or two of sleep() after iscsiadm creates the device. The sleep can be done in PHP instead of in the exec() of the shell.

溺渁∝ 2024-10-15 04:27:44

我通过在这段代码和给我“单位”的代码之间休眠来修复它

I fix it with a sleep between this code and the code that gave me the "unit"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文