如何通过 SSH 运行 php 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/612115/
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 do I run a php script through SSH?
提问by Mez
I have a rather long php script and whenever my internet connection skips out for a second the browser seems to stop the script. I can't sit around for 8 hours for my script to run so I figured I could just run it via ssh and be come back the next day and get my output file. However simple typing the scripts name into ssh doesn't seem to work. is there a special command to run php scripts through ssh?
我有一个相当长的 php 脚本,每当我的互联网连接跳出一秒钟时,浏览器似乎都会停止脚本。我不能坐 8 个小时让我的脚本运行,所以我想我可以通过 ssh 运行它,然后第二天回来获取我的输出文件。然而,简单地将脚本名称输入 ssh 似乎不起作用。是否有一个特殊的命令可以通过 ssh 运行 php 脚本?
回答by Mez
If your internet connection is dropping out, then this is probably going to be a problem across SSH as well, and well, having an SSH window open isn't always the best thing to do (what happens if you accidentally close the ssh window?)
如果您的互联网连接中断,那么这也可能是 SSH 中的一个问题,而且,打开 SSH 窗口并不总是最好的做法(如果您不小心关闭了 ssh 窗口会发生什么? )
I would suggest SSHing into the server, then running a program called "screen", which will keep on running whatever you run inside of it, even if your connection drops.
我建议通过 SSH 连接到服务器,然后运行一个名为“ screen”的程序,它会继续运行您在其中运行的任何内容,即使您的连接断开。
To do this, first ssh into the server, and type
为此,首先通过 ssh 进入服务器,然后键入
screen
screen
This will load up screen, hit enter to bypass the welcome screen
这将加载屏幕,按回车键绕过欢迎屏幕
Now, run your PHP script
现在,运行你的 PHP 脚本
php /path/to/your/php/script.php
php /path/to/your/php/script.php
this will start PHP running,
这将开始运行 PHP,
You can now close the window if you want, and the script will keep on running
您现在可以根据需要关闭窗口,脚本将继续运行
To get back to your screen session, connect to the server, and run the command
要返回您的屏幕会话,请连接到服务器,然后运行命令
screen -raAD
screen -raAD
which will reconnect you to your session, as if you'd had the window open all the time.
这将使您重新连接到您的会话,就像您一直打开窗口一样。
This is actually pretty good for running long winded scripts, or even for running a console based IRC session :D
这实际上非常适合运行冗长的脚本,甚至对于运行基于控制台的 IRC 会话 :D
回答by Mark Biek
In general, you should be able to run the script from the command-line like this
通常,您应该能够像这样从命令行运行脚本
php myscript.php
Doing it on a remote host via ssh could be done like this
可以通过 ssh 在远程主机上执行此操作
ssh [email protected] "php myscript.php"
回答by Mark Biek
In addition to what everyone else said, you'll probably want to use nohupas well.
除了其他人所说的,您可能也想使用nohup。
ssh user@host "nohup php script.php"
That way it'll keep running even if your sshconnection drops. You could also use screenin place of nohupif you want.
这样,即使您的ssh连接断开,它也会继续运行。如果需要,您也可以使用screen代替nohup。
回答by Ikke
try
尝试
php script.php.
If that doesn't work, you would have to locate the php executable and then execute it.
如果这不起作用,您必须找到 php 可执行文件然后执行它。
Btw, you can use screen, so if the connection to the computer is lost, the script still runs.
顺便说一句,您可以使用屏幕,因此如果与计算机的连接丢失,脚本仍会运行。
回答by Greg
Alternatively you can just call ignore_user_abort()and close your browser after you hit the page.
或者,您可以ignore_user_abort()在点击页面后调用并关闭浏览器。
回答by Greg
nohup php -q /path-to-script/file.php & exit
nohup php -q /path-to-script/file.php & exit
will execute the php file and close the console without terminating the process.
将执行 php 文件并关闭控制台而不终止进程。

