简体   繁体   English

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

[英]Running PHP script from command line as background process

I'm trying to run a PHP script continually in the background via the command line in Linux.我正在尝试通过 Linux 中的命令行在后台持续运行 PHP 脚本。 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.我已经尝试过命令php filename.php &但似乎脚本执行很快终止,而它应该继续运行直到进程终止。

Any suggestions?有什么建议么?

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);

at the top of your script to display any errors it may have, then you can use: 在脚本的顶部显示它可能有的任何错误,然后您可以使用:

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 &

Same as above but doesn't create nohup.out file. 与上面相同但不创建nohup.out文件。


You can also use: 您还可以使用:
ignore_user_abort(1);

Set whether a client disconnect should abort script execution 设置客户端断开连接是否应中止脚本执行


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 php and the filename.php paths may be provided as a full-path , instead of php and filename.php , you can use /usr/bin/php and /full/path/to/filename.php . phpfilename.php路径可以作为完整路径提供 ,而不是phpfilename.php ,你可以使用/usr/bin/php/full/path/to/filename.php
Full Path is recommended to avoid file not found errors. 建议使用完整路径以避免文件未找到错误。

the process may be closed when your session is closed. 您的会话结束时,该流程可能会被关闭。

try using nohup php filename.php 尝试使用nohup php filename.php

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. 它们的内置编号按顺序为0, 1, and 2 By default, if you don't name or number one explicitly, you're talking about STDOUT . 默认情况下,如果您没有明确地命名或编号,那么您正在谈论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/

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

Where: 哪里:
command-name : name of shell script or command name. command-name :shell脚本或命令名称。 You can pass argument to command or a shell script. 您可以将参数传递给命令或shell脚本。
& : nohup does not automatically put the command it runs in the background; & :nohup不会自动将其运行的命令放在后台; you must do that explicitly, by ending the command line with an & symbol. 您必须通过使用&符号结束命令行来明确地执行此操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM