php - 从 PHP 在后台运行 shell 脚本

标签 php linux bash shell

场景:

  1. 我有一个 PHP 脚本(通过网络访问),它生成一个 shell 脚本,即使在请求结束后也需要继续运行。
  2. 我需要将输出发送到磁盘上的文件(即“./script.sh > run.log”)

我尝试的每一种方法似乎都会导致 PHP 仍在等待 shell 脚本完成执行。我尝试过的方法:

  1. nohup ./script.sh > run.log &
  2. nohup ./script.sh > run.log 2>&1
  3. nohup ./script.sh > run.log 2>&1 &
  4. ./script.sh &
  5. ./script.sh > run.log 2>&1
  6. ./script.sh > run.log &

我认为我可能遗漏了一些非常明显的东西。有什么想法吗?

最佳答案

我认为您可以通过使用脚本启动一个 screen 实例来实现这一点:

screen -d -m 'bash ./script.sh > run.log'

关于php - 从 PHP 在后台运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209175/

相关文章:

php - 在 PHP 中获取 stdClass 对象的名称

javascript - 检查日期选择器 html 元素中的值是否为空并执行 jquery 函数

linux - 在 virtualbox 中从 Ubuntu 14.04 桌面版本制作网络服务器

linux - 使用另一个 bash 命令输出的 bash 命令

javascript - 如何在网页上显示状态消息

php - 在逗号分隔值字段 Laravel 5.1 中查找

php - 为什么我必须使用 PDO 启动事务,然后提交它才能从 MySQL DB 中删除数据

linux - 在linux上安装phpsh,Python报错

linux - 在 Linux 上将 anaconda 安装到与 home 不同的文件夹

bash - 使用 Bash 将给定当前目录的绝对路径转换为相对路径