Shell_exec php 与 nohup

发布于 2024-10-16 08:15:38 字数 813 浏览 6 评论 0原文

我认为类似的帖子有很多,但在搜索后我还没有找到解决方案。

基本上,我试图在后台运行两个脚本。当我在命令行中运行它们时,我在调用第一个脚本后看到:

/usr/bin/nohup php script.php > nohupoutput.log & echo $!

我已经尝试过 ...script.php > /dev/null & 具有相同的结果。我得到:

/usr/bin/nohup: ignoring input and redirecting stderr to stdout

我忽略并运行第二个。我注意到它似乎挂在那里,按 Enter 让我回到 machine:~folder>

/usr/bin/nohup php script2.php > nohupoutput.log & echo $!

两个脚本都可以工作。我尝试将其转换为 shell_exec 命令,但似乎没有任何效果。我怀疑忽略输入位造成了困难,但我不确定。不管怎样,下面的方法是行不通的。它只是挂在浏览器中:

$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');

I think there are tons of similar posts but I haven't yet found a solution after searching around.

Basically, I'm trying to run two scripts in the background. When I run them in the commandline, I see after calling my first script:

/usr/bin/nohup php script.php > nohupoutput.log & echo $!

I've tried ...script.php > /dev/null & with the same result. I get:

/usr/bin/nohup: ignoring input and redirecting stderr to stdout

which I ignore and run the second one. I noticed that it seemed to be hanging there, and pressing Enter brought me back to machine:~folder>

/usr/bin/nohup php script2.php > nohupoutput.log & echo $!

Both scripts work. I tried to then convert this to a shell_exec command and nothing seems to work. I suspect that the ignoring input bit is causing difficulties, but I'm not sure. Regardless, the following does not work. It just hangs in the browser:

$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');

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

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

发布评论

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

评论(4

丿*梦醉红颜 2024-10-23 08:15:38

尝试:

$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

或:

exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

Try:

$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

Or:

exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
盛装女皇 2024-10-23 08:15:38

这应该有效:

shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');

This shoul work:

shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');
无悔心 2024-10-23 08:15:38
<?php
function execInBackground($cmd) {
    if (substr(php_uname(), 0, 7) == "Windows"){
        pclose(popen("start /B ". $cmd, "r")); 
    }
    else {
        exec($cmd . " > /dev/null &");  
    }
}

// take note: to get your PHP_PATH, try looking at your phpinfo :)
echo execInBackground("/usr/local/php53/bin/php 'example2.php'");
?>
<?php
function execInBackground($cmd) {
    if (substr(php_uname(), 0, 7) == "Windows"){
        pclose(popen("start /B ". $cmd, "r")); 
    }
    else {
        exec($cmd . " > /dev/null &");  
    }
}

// take note: to get your PHP_PATH, try looking at your phpinfo :)
echo execInBackground("/usr/local/php53/bin/php 'example2.php'");
?>
一念一轮回 2024-10-23 08:15:38

首先将 php 命令放入 shell 文件脚本中,例如 myscript.sh

#!/bin/bash
# myscript.sh file
php script.php

使用 myscript.sh 运行 nohup:

sudo nohup ./myscript.sh &

使用 ps 验证:

ps aux | grep myscript.sh

First put your php command in a shell file script, e.g. myscript.sh:

#!/bin/bash
# myscript.sh file
php script.php

Run nohup with myscript.sh:

sudo nohup ./myscript.sh &

Verify with ps:

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