Linux 从命令行运行 PHP 脚本作为后台进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6989161/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:35:34  来源:igfitidea点击:

Running PHP script from command line as background process

phplinuxcommand-line

提问by Kyle

I'm trying to run a PHP script continually in the background via the command line in Linux. I have tried the command php filename.php &but it seems like the script execution terminates very quickly, while it should keep running until the process is terminated.

我正在尝试通过 Linux 中的命令行在后台连续运行 PHP 脚本。我已经尝试过该命令,php filename.php &但似乎脚本执行很快终止,而它应该继续运行直到进程终止。

Any suggestions?

有什么建议?

采纳答案by CONvid19

Are you sure the script doesn't contain any errors? This is what normally makes "execution terminates very quickly".
First, append:

您确定该脚本不包含任何错误吗?这就是通常使“执行很快终止”的原因。
首先,附加:

error_reporting(E_ALL); ini_set('display_errors', 1);

error_reporting(E_ALL); ini_set('display_errors', 1);

at the top of your script to display any errors it may have, then you can use:

在脚本顶部显示它可能存在的任何错误,然后您可以使用:

nohup php filename.php &

nohup php filename.php &

nohup runs a command even if the session is disconnected or the user logs out.

即使会话断开或用户注销,nohup 也会运行命令。

OR

或者

nohup php filename.php >/dev/null 2>&1 &

nohup php filename.php >/dev/null 2>&1 &

Same as above but doesn't create nohup.outfile.

同上,但不创建nohup.out文件。



You can also use:
ignore_user_abort(1);

您还可以使用:
ignore_user_abort(1);

Set whether a client disconnect should abort script execution

设置客户端断开连接是否应中止脚本执行


set_time_limit(0);


set_time_limit(0);

Limits the script maximum execution time, in this case it will run until the process finishes or the apache process restarts.

限制脚本最大执行时间,在这种情况下它会一直运行到进程完成或 apache 进程重新启动。



Notes

笔记

The phpand the filename.phppaths may be provided as a full-path, instead of phpand filename.php, you can use /usr/bin/phpand /full/path/to/filename.php.
Full Path is recommendedto avoid file not founderrors.

phpfilename.php路径可以被设置为一个完整路径,而不是phpfilename.php,你可以使用/usr/bin/php/full/path/to/filename.php
建议使用完整路径以避免文件未找到错误。

回答by mustafa

the process may be closed when your session is closed.

当您的会话关闭时,该进程可能会关闭。

try using nohup php filename.php

尝试使用 nohup php filename.php

回答by Yash Tomar -Aeologic

nohup /path/to/command-name arg1 arg2 &

Where:
command-name: name of shell script or command name. You can pass argument to command or a shell script.
&: nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.

其中::
command-nameshell 脚本的名称或命令名称。您可以将参数传递给命令或 shell 脚本。
&: nohup 不会自动把它运行的命令放到后台;你必须明确地做到这一点,用 & 符号结束命令行。

回答by Kishan Rajdev

nohup php file.php > /dev/null 2>&1 &

The greater-thans (>) in commands like these redirect the program's output somewhere. In this case, something is being redirected to /dev/null, and something is being redirected to &1

>这些命令中的大于号 ( ) 将程序的输出重定向到某处。在这种情况下,有些东西被重定向到/dev/null,有些东西被重定向到&1

Standard in, out, and error

标准输入、输出和错误

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it's an interactive program, or from another program if it's processing the other program's output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

一个程序有三个标准的输入和输出源。标准输入通常来自键盘,如果它是一个交互式程序,或者来自另一个程序,如果它正在处理另一个程序的输出。程序通常打印到标准输出,有时打印到标准错误。这三个文件描述符(您可以将它们视为“数据管道”)通常称为STDIN, STDOUT, and STDERR.

Sometimes they're not named, they're numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don't name or number one explicitly, you're talking about STDOUT.

有时他们没有名字,他们有编号!它们的内置编号是0, 1, and 2,按此顺序。默认情况下,如果您没有明确命名或排名第一,那么您在谈论STDOUT.

the command above is redirecting the standard output to /dev/null, which is a place you can dump anything you don't want, then redirecting standard error into standard output (you have to put an &in front of the destination when you do this).

上面的命令将标准输出重定向到/dev/null,这是您可以转储不需要的任何内容的地方,然后将标准错误重定向到标准输出(&执行此操作时必须在目标前面放置)。

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That's one good way to make a program be really quiet!

因此,简短的解释是“该命令的所有输出都应该被推入黑洞。” 这是使程序真正安静的一种好方法!

&at the end puts the command in background.

&最后将命令置于后台。

ref: https://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/

参考:https: //www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/