使用 crontab 执行 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2689284/
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
Executing php with crontab
提问by Stefan Konno
I'm trying to run a php-script on a scheduled basis. So I'd thought crontab was a good idea. The server I'm using is on Linux which I'm not that familiar with. So the problem I'm having is, I don't know how make the script executable from php. I need to reference the script, or put it into a folder that can run php from the command line. So I don't know what path to give my crontab, for example:
我正在尝试按计划运行 php 脚本。所以我认为 crontab 是个好主意。我使用的服务器在我不太熟悉的 Linux 上。所以我遇到的问题是,我不知道如何使脚本从 php 可执行。我需要引用该脚本,或者将其放入可以从命令行运行 php 的文件夹中。所以我不知道给我的 crontab 提供什么路径,例如:
5 * * * * var/www/some/path/script.php
I found some vague information about this php executable being found in
我发现了一些关于这个 php 可执行文件的模糊信息
/usr/bin/php
But I can't find any php file in there, maybe I don't have it installed? My php5 and apache installation is in:
但是我在那里找不到任何 php 文件,也许我没有安装它?我的 php5 和 apache 安装在:
/etc/php5
So my question becomes, is there anyway to execute a php-script with crontab in any other folder, or do I just lack the php executable in usr/bin/php?
所以我的问题变成了,是否有任何其他文件夹中的 crontab 执行 php 脚本,或者我只是在 usr/bin/php 中缺少 php 可执行文件?
回答by richsage
Start by typing at a command line:
首先在命令行输入:
whereis php
whereis php
Do this as the user that the cron job will be run under. This will show you the path to your executable. You can then use that path (if it's not already in your PATH variable) in your cron entry:
作为将在其下运行 cron 作业的用户执行此操作。这将向您显示可执行文件的路径。然后,您可以在 cron 条目中使用该路径(如果它不在您的 PATH 变量中):
5 * * * * /your/path/to/php /var/www/some/path/script.php
5 * * * * /your/path/to/php /var/www/some/path/script.php
Edit:you may need to install the php5-cli(Ubuntu package name) package if all you have is the Apache PHP module installed. This will give you the binary executable that you can run from a command line.
编辑:php5-cli如果你只安装了 Apache PHP 模块,你可能需要安装(Ubuntu 包名称)包。这将为您提供可以从命令行运行的二进制可执行文件。
回答by techHymaner
I had to find to follow a trail to find the executable:
我必须找到跟踪路径才能找到可执行文件:
andy@ararat:~$ type php
php is /usr/bin/php
andy@ararat:~$ file /usr/bin/php
/usr/bin/php: symbolic link to `/etc/alternatives/php'
andy@ararat:~$ file /etc/alternatives/php
/etc/alternatives/php: symbolic link to `/usr/bin/php5'
andy@ararat:~$ file /usr/bin/php5
/usr/bin/php5: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, stripped
so you need to include /usr/bin/php5 as the path to the php executable like so:
所以你需要包含 /usr/bin/php5 作为 php 可执行文件的路径,如下所示:
andy@ararat:~$crontab -e
#*/1 * * * * /usr/bin/php5 /home/andy/www/dev.com/corp_rewards_cron.php
回答by rubber boots
Is this a Linux system?
这是Linux系统吗?
In newer Linux distributions, there is
actually a convienient crontab-setup system
that doesn't require any entry in the crontab
by the user.
E.g in SuSE Linux, you have directories
在较新的 Linux 发行版中,
实际上有一个方便的 crontab 设置系统
,不需要用户在 crontab 中输入任何内容。例如,在 SuSE Linux 中,您有目录
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.monthly/
/etc/cron.weekly/
Just put an invocation script (konno_php_start) in any of these directories, like
只需在这些目录中的任何一个目录中放置一个调用脚本(konno_php_start),例如
/etc/cron.hourly/konno_php_start
which is executable (chmod 755 or so) and contains:
它是可执行的(chmod 755 左右)并包含:
#!/bin/sh
cd /var/www/some/path/
php script.php >> logfile.txt 2>&1
and restart the cron daemon. Thats it.
并重新启动 cron 守护进程。就是这样。
From the logfile, you'll see if your php interpreter
will be found in the PATH. If not, change the
line in /etc/cron.hourly/konno_php_start
to
从日志文件中,您将看到您的 php 解释器
是否会在 PATH 中找到。如果没有,请将 /etc/cron.hourly/konno_php_start 中的行更改为
/full/path/to/php script.php >> logfile.txt 2>&1
Regards
问候
rbo
红包
回答by Etienne Martin
You can use the wget command locally:
您可以在本地使用 wget 命令:
5 * * * * wget http://localhost/some/path/script.php
回答by Victor Stanciu
You can also use env, it will find and launch php for you:
您也可以使用env,它会为您找到并启动 php:
/usr/bin/env php /var/www/some/path/script.php
Or you can place a shebang in your script.php (first line):
或者你可以在你的 script.php 中放置一个shebang(第一行):
#!/usr/bin/env php
then make it executable, and make crontab call it directly, like in your first example:
然后使其可执行,并让 crontab 直接调用它,就像在你的第一个例子中一样:
5 * * * * /var/www/some/path/script.php
回答by Nazrul Muhaimin
I suggest that you do like this,
我建议你这样做,
*/5 * * * * /path/gridmon2.pl 1> /dev/null 2> /dev/null
where in you .pl code you should grep using wget or something like this:
在你的 .pl 代码中,你应该使用 wget 或类似的东西 grep :
wget "/www/root/index.php"
or you can just do like this:
或者你可以这样做:
/usr/bin/wget "/www/root/index.php"
It's just my suggestion, I've only try wget to external site not locally and it works.
这只是我的建议,我只尝试 wget 到外部站点而不是本地站点并且它有效。
please try and revert.
请尝试并恢复。

