我应该使用“|”吗?现在”或与号 (&) 在后台运行脚本?
我一直在查看有关在后台运行 php 脚本的答案,它们似乎以两种方式传播。
有些人建议使用这个(或类似的东西):
/usr/bin/php command >/dev/null 2>&1 &
其他人建议使用“at”命令:
echo '/usr/bin/php command'| at now
这两种方法的主要区别是什么?有什么优点和缺点?
我想做的是,当用户提交表单时,我需要运行一个几分钟长的脚本,显然应该在后台运行。我已经尝试了两种方法,它们都对我有用,但我不确定该选择哪一种。
I've been looking through the answers about running php script in a background and they seem to be spreading in two ways.
Some people suggest using this (or something similar):
/usr/bin/php command >/dev/null 2>&1 &
Other suggest using "at" command:
echo '/usr/bin/php command'| at now
What are the major differences about theese two methods? What are the pros and cons?
What I'm trying to do is when user submits the form, I need to run a few-minutes long script, that should obviously be run in background. I've tried both ways and they both worked for me, but I'm not sure which to pick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
at 命令是一个调度程序,它接受来自标准输入的字符串或包含在特定时间运行的命令的文件。您的示例:
将“at”命令作为字符串提供给您的命令并安排它立即运行。
第一种方法是通过 shell 将进程后台化的典型方法:
“> /dev/null”部分告诉 shell 将命令的标准输出发送到 /dev/null,“2>”部分告诉 shell 将命令的标准输出发送到 /dev/null。 &1" 的一部分是说将命令的 stderr 输出发送到 stdout (然后转到 /dev/null)。它们一起抑制输出。 一步完成此操作:
实际上,您可以使用“&” 最后告诉 shell 将进程置于后台。
使用“at”的优点是可以灵活地安排命令在当前以外的其他时间运行(除其他外)。阅读它的手册页。缺点是它可能无法安装在所有系统上。
使用 & 的优点通过 shell 的优点是后台本身没有任何开销。使用“at”立即运行命令有点矫枉过正,因为它涉及为 at 创建一个新进程、调度该命令、意识到它已设置为立即运行,然后在后台运行它。而使用“&”运行命令shell 中只会在后台运行该命令。
The at command is a scheduler that accepts strings from stdin or files that contain commands to run at particular times. Your example:
Is giving the 'at' command your command as a string and scheduling it to be run immediately.
The first method is the the typical way to background a process via the shell:
The "> /dev/null" part of that is telling the shell to send the stdout of your command to /dev/null, and the "2>&1" part of that is saying to send the output of stderr of your command to stdout (which then goes to /dev/null). Together, they suppress output. You can actually do that in one step with:
The '&' at the end of that is what tells the shell to background the process.
The pros of using 'at' is the flexability of scheduling your command to be run at other times than now (among other things). Read the man page for it. The cons are that it may not be installed on all systems.
The pros of using & via shells is that there is no overhead on the backgrounding per se. When using 'at' to run a command immediately is kindof overkill, since it involve creating a new process for at, scheduling the command, realizing that it's set to be run now, and then running it in the background. Whereas running the command with "&" in the shell will simply run that command in the background.
当您使用
at
命令调用它时,该程序将由at
守护进程在后台执行。当您使用
&
时,进程仍然附加到当前 shell。当您关闭 shell 时,该进程将终止。您还可以执行 nohoup /usr/bin/php 命令,以便在您关闭 shell 时该进程继续运行。When you call it with the
at
command the program is executed in the background by theat
daemon.When you use
&
the process is still attached to the current shell. When you close the shell, the process is terminated. You could also do anohoup /usr/bin/php command
so the process keeps running when you close your shell.