如何通过命令行运行 php 脚本(并在注销后保持运行)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/575345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run a php script through the command line (and keep it running after logging out)
提问by Vinayak
I am trying to run a php script on my remote Virtual Private Server through the command line. The process I follow is:
我正在尝试通过命令行在我的远程虚拟专用服务器上运行一个 php 脚本。我遵循的过程是:
- Log into the server using PuTTY
- On the command line prompt, type> php myScript.php
- 使用 PuTTY 登录服务器
- 在命令行提示符下,键入> php myScript.php
The script runs just fine. BUT THE PROBLEM is that the script stops running as soon as I close the PuTTY console window.
脚本运行得很好。但问题是,一旦我关闭 PuTTY 控制台窗口,脚本就会停止运行。
I need the script to keep on running endlessly. How can I do that? I am running Debian on the server.
我需要脚本继续无休止地运行。我怎样才能做到这一点?我在服务器上运行 Debian。
Thanks in advance.
提前致谢。
回答by Steve Weet
I believe that Ben has the correct answer, namely use the nohup command. nohup stands for nohangup and means that your program should ignore a hangup signal, generated when you're putty session is disconnected either by you logging out or because you have been timed out.
我相信 Ben 有正确的答案,即使用 nohup 命令。nohup 代表 nohangup,意思是你的程序应该忽略挂断信号,当你的腻子会话因你注销或因为你超时而断开连接时产生。
You need to be aware that the output of your command will be appended to a file in the current directory named nohup.out (or $HOME/nohup.out if permissions prevent you from creating nohup.out in the current directory). If your program generates a lot of output then this file can get very large, alternatively you can use shell redirection to redirect the output of the script to another file.
您需要注意,您的命令的输出将附加到当前目录中名为 nohup.out 的文件中(如果权限阻止您在当前目录中创建 nohup.out,则为 $HOME/nohup.out)。如果您的程序生成大量输出,那么此文件可能会变得非常大,或者您可以使用 shell 重定向将脚本的输出重定向到另一个文件。
nohup php myscript.php >myscript.output 2>&1 &
This command will run your script and send all output (both standard and error) to the file myscript.output which will be created anew each time you run the program.
此命令将运行您的脚本并将所有输出(标准和错误)发送到文件 myscript.output,每次运行程序时都会重新创建该文件。
The final & causes the script to run in the background so you can do other things whilst it is running or logout.
最后的 & 使脚本在后台运行,因此您可以在它运行或注销时做其他事情。
回答by Ben
An easy way is to run it though nohup:
一个简单的方法是通过 nohup 运行它:
nohup php myScript.php &
回答by ConroyP
If you run the php command in a screen, detach the screen, then it won't terminate when you close your console.
如果您在 a 中运行 php 命令screen,将 分离screen,那么当您关闭控制台时它不会终止。
Screen is a terminal multiplexer that allows you to manage many processes through one physical terminal. Each process gets its own virtual window, and you can bounce between virtual windows interacting with each process. The processes managed by screen continue to run when their window is not active.
Screen 是一个终端多路复用器,它允许您通过一个物理终端管理多个进程。每个进程都有自己的虚拟窗口,您可以在与每个进程交互的虚拟窗口之间跳转。由 screen 管理的进程在其窗口未处于活动状态时继续运行。

