PHP执行命令和日志输出,无需等待 [英] PHP execute command and log output without waiting

查看:510
本文介绍了PHP执行命令和日志输出,无需等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用exec()执行Linux或Windows命令.

I use exec() to execute command, either linux or windows.

如何执行命令,Linux和Windows,并在不等待的情况下记录输出?

How do you execute a command, linux and windows, and log the output without waiting?

对于Linux,我知道不要等待输出:command* > /dev/null 2>/dev/null &

I know for linux, to not wait for the output: command* > /dev/null 2>/dev/null &

并记录Linux的输出:command* > /path/to/log.txt 2>/path/to/error.txt

And to log output for linux: command* > /path/to/log.txt 2>/path/to/error.txt

如何使用一个命令记录日志并将其设置为后台?窗户看起来如何?

How would you go about logging and setting it to background in one command? How would windows look like too?

推荐答案

在Linux上,您可以执行以下操作:

On Linux you can do:

exec('command* > /dev/null 2>/dev/null &');

在Windows上,您可以执行以下操作:

On Windows you can do:

pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));

这两个示例均禁用输出和错误,这些错误将输出到/dev/null(linux)或NUL(windows),这意味着它们无处存储".

Both examples disable output and errors, those go to /dev/null (linux) or NUL (windows) which means they are stored "nowhere".

您可以将它们替换为系统上的有效路径.

You can replace these with valid paths on your system.

在Linux上,最后的&将其置于后台.在Windows上,这更加复杂,需要 start 来调用该过程,并需要

On Linux, a & at the end places it into background. On windows this is more complicated and needs start to invoke the process and cmd to allow redirection of the streams.

这篇关于PHP执行命令和日志输出,无需等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆