Bash 脚本来运行 php 脚本

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

Bash script to run php script

phpbashshellcron

提问by mmmbaileys

I have a php script that I want to be run using a bash script, so I can use Cron to run the php script every minute or so.

我有一个 php 脚本,我想使用 bash 脚本运行它,所以我可以使用 Cron 每分钟左右运行一次 php 脚本。

As far as I'm aware I need to create the bash script to handle the php script which will then allow me to use the Cron tool/timer.

据我所知,我需要创建 bash 脚本来处理 php 脚本,然后我才能使用 Cron 工具/计时器。

So far I was told I need to put:

到目前为止,我被告知我需要输入:

#!/pathtoscript/testphp.php

at the start of my php script. Im not sure what to do from here...

在我的 php 脚本的开头。我不知道从这里做什么...

Any advice? Thanks.

有什么建议吗?谢谢。

回答by Rafe Kettler

If you have PHP installed as a command line tool (try issuing phpto the terminal and see if it works), your shebang (#!) line needs to look like this:

如果您将 PHP 安装为命令行工具(尝试向php终端发出命令并查看它是否有效),您的 shebang ( #!) 行需要如下所示:

#!/usr/bin/php

Put that at the top of your script, make it executable (chmod +x myscript.php), and make a Cron job to execute that script (same way you'd execute a bash script).

将它放在脚本的顶部,使其可执行 ( chmod +x myscript.php),然后创建一个 Cron 作业来执行该脚本(与执行 bash 脚本的方式相同)。

You can also use php myscript.php.

您也可以使用php myscript.php.

回答by FDisk

#!/usr/bin/env bash
PHP=`which php`
$PHP /path/to/php/file.php

回答by Alex Gray

A previous poster said..

以前的海报说..

If you have PHP installed as a command line tool… your shebang (#!) line needs to look like this: #!/usr/bin/php

如果您将 PHP 安装为命令行工具……您的 shebang (#!) 行需要如下所示: #!/usr/bin/php

While this couldbe true… just because you can type in phpdoes NOT necessarily mean that's where php is going to be... /usr/bin/phpis A common location… but as with any shebang… it needs to be tailored to YOUR env.

虽然这可能是真的......仅仅因为你可以输入php并不一定意味着这就是 php 的 /usr/bin/php位置......是一个常见的位置......但是对于任何shebang......它需要针对你的env.

a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.. ?which -a phpENTER, which for me looks like..

一种快速找出您的特定可执行文件在您的$PATH..上的位置的方法,试试..?which -a phpENTER,对我来说看起来像..

php is /usr/local/php5/bin/php
php is /usr/bin/php
php is /usr/local/bin/php
php is /Library/WebServer/CGI-Executables/php

php is /usr/local/php5/bin/php
php is /usr/bin/php
php is /usr/local/bin/php
php is /Library/WebServer/CGI-Executables/php

The first one is the default i'd get if I just typed in php at a command prompt… but I can use any of them in a shebang, or directly… You can also combine the executable name with env, as is often seen, but I don't really know much about / trust that. XOXO.

如果我只是在命令提示符下输入 php,第一个是我得到的默认值……但我可以在 shebang 中使用它们中的任何一个,或者直接使用……您也可以将可执行文件名称与 组合在一起env,就像经常看到的那样,但是我不太了解/相信那个。XOXO。

回答by Sergio Morales

I'm pretty sure something like this is what you are looking for:

我很确定这样的事情就是你要找的:

#!/bin/sh

php /pathToScript/script.php

Save that with your desired script name (such as runPHP.sh) and give it execution rights, then you can use it however you want.

使用您想要的脚本名称(例如 runPHP.sh)保存它并赋予它执行权限,然后您可以随意使用它。

Edit: You might as well not use a bash script at all and just add the "php ..." command to the crontab, if I'm not mistaken.

编辑:如果我没记错的话,您最好根本不使用 bash 脚本,只需将“php ...”命令添加到 crontab。

Good luck!

祝你好运!

回答by Spyros

You just need to set :

你只需要设置:

/usr/bin/php path_to_your_php_file

in your crontab.

在你的 crontab 中。

回答by Bogdan Constantinescu

The bash script should be something like this:

bash 脚本应该是这样的:

#!/bin/bash
/usr/bin/php /path/to/php/file.php

You need the php executable (usually found in /usr/bin) and the path of the php script to be ran. Now you only have to put this bash script on crontab and you're done!

您需要 php 可执行文件(通常在 /usr/bin 中找到)和要运行的 php 脚本的路径。现在你只需要把这个 bash 脚本放在 crontab 上就大功告成了!

回答by Eddie B

a quick way to find out WHERE YOUR particular executable is located on your $PATH, try.

一种快速找出您的特定可执行文件在 $PATH 上的位置的方法,请尝试。

Even quicker way to find out where phpis ...

更快速的方法来找出在哪里php......

whereis php

I'm running debianand above command showing me

我正在运行debian和上面的命令显示我

php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz

Hope that helps.

希望有帮助。

回答by Costi Ciudatu

If you don't do anything in your bash script than run the php one, you could simply run the php script from cron with a command like /usr/bin/php /path/to/your/file.php.

如果你在 bash 脚本中除了运行 php 之外什么都不做,你可以简单地从 cron 使用 /usr/bin/php /path/to/your/file.php 这样的命令运行 php 脚本。

回答by Luis H Cabrejo

I found php-cgi on my server. And its on environment path so I was able to run from anywhere. I executed succesfuly file.phpin my bash script.

我在我的服务器上找到了 php-cgi。它在环境路径上,所以我可以从任何地方运行。我file.php在我的 bash 脚本中成功执行。

#!/bin/bash
php-cgi ../path/file.php

And the script returned this after php script was executed:

并且脚本在执行 php 脚本后返回了这个:

X-Powered-By: PHP/7.1.1 Content-type: text/html; charset=UTF-8

X-Powered-By:PHP/7.1.1 内容类型:text/html;字符集=UTF-8

done!

完毕!

By the way, check first if it works by checking the version issuing the command php-cgi -v

顺便说一句,首先通过检查发出命令的版本来检查它是否有效 php-cgi -v