使用crontab运行PHP脚本 [英] Running a PHP script with crontab

查看:115
本文介绍了使用crontab运行PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是为了问题,但无论我查看了多少教程,我不能让我的 crontab 工作,我正在建立一个网站,将依靠 crontab 每天晚上在数据库中重置特定设置。



这里是我的 crontab 文件:

 #编辑此文件以介绍cron要运行的任务。 

#每个要运行的任务都必须通过单个行来定义
#指示当任务将运行时使用不同的字段
#和什么命令运行任务

#要定义时间,您可以为
提供具体的值:#分钟(m),小时(h),月(dom),月#
#注意,任务将根据cron的系统启动
#守护进程的时间概念和时区。

#crontab作业的输出(包括错误)通过
#email发送给crontab文件所属的用户(除非重定向)。

#例如,您可以在每周的上午5点运行所有用户帐户的备份
#:
#0 5 * * 1 tar -zcf / var /backups/home.tgz / home /

#有关详细信息,请参见crontab(5)和cron(8)的手册页面

#mh dom命令
* * * * * / usr / bin / php -q /var/www/html/cron/index.php

如果我尝试 cd / usr / bin / php / p>

  -bash:cd:/ usr / bin / php:不是目录
/ pre>

所以我 cd 'd到 / usr / bin / code>这是我发现:

  -rwxr-xr-x 1 root root 27216 Feb 10 15: 08 pgrep * 
lrwxrwxrwx 1 root root 21 Jun 13 09:36 php - > / etc / alternatives / php *
-rwxr-xr-x 1 root root 9049256 7月2日11:57 php5 *

如果我 cd / etc / alternatives ,我找到:

  lrwxrwxrwx 1 root root 13 Jun 13 09:36 php  - > / usr / bin / php5 * 

我回到 code>文件, php5 具有 * 符号且为绿色。

  -rwxr-xr-x 1根根9049256 7月2日11:57 php5 * 

我的PHP脚本。很简单。检查cookie,如果存在,则增加一个。然后我检查另一个页面上的结果。手动这工作。 crontab ,无法让它工作。

  if ($ _COOKIE ['cronTest'])){
$ int = $ _COOKIE ['cronTest'];
$ int ++;
setcookie(cronTest,$ int,time()+ 3600);
}


解决方案


  • 很可能, / var / www / html / cron 中的脚本由 www-data 用户。根据您的设置,执行cronjob的用户没有运行此文件的权限。


  • 命令行中没有$ _COOKIE。 Cookie由用户浏览器发送。由于cli不是浏览器,因此您无法读取Cookie值。虽然你可以访问用户$ _SESSION,但这是另一个故事。看看这里是它可以通过命令提示符执行PHP5脚本读取cookie /会话值?以获取更多详细信息。




您的cronjob行似乎有效,因此问题将是上述之一。要验证第一个,在你的文件中尝试不使用$ _COOKIE,就像

  mkdir('/ var / www / html / cron / testdir'); 

只是为了查看是否可以访问该文件,并在dir中创建一个目录。如果无法访问,请创建一个新组并添加当前用户(查找

  ls -al $ / var / www / html / cron中的b $ b  

)和运行cronjob到组的用户,然后make该组拥有您要运行的文件。请参阅接受此问题的答案:设置用户权限| Artisan命令不会在代码中运行,但在命令行上正常工作我发布了一些时间回来如何做到这一点。



对于$ _COOKIE问题,您必须找到另一个解决方案。例如,使用Redis或Memcached作为可以通过online和cli配置访问的缓存服务。



考虑添加

  1> / dev / null 2>& 1 

到cronjob行的末尾,这样做,让cron每分钟运行,你会得到大量的日志文件。



为了在使用php和cli时可能出现的错误,请确保为您的文件提供完整路径。


I understand SO is for questions but no matter how many tutorials I have looked up, I cannot get my crontab to work and I am building an website that will rely on crontab to reset a particular setting in my database every night.

Here is my crontab file:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* * * * * /usr/bin/php -q  /var/www/html/cron/index.php

If I try to cd to /usr/bin/php i get

-bash: cd: /usr/bin/php: Not a directory

So I cd'd to just /usr/bin/ and this is what i found:

-rwxr-xr-x  1 root   root      27216 Feb 10 15:08 pgrep*
lrwxrwxrwx  1 root   root         21 Jun 13 09:36 php -> /etc/alternatives/php*
-rwxr-xr-x  1 root   root    9049256 Jul  2 11:57 php5*

If I cd to /etc/alternatives I find:

lrwxrwxrwx  1 root root   13 Jun 13 09:36 php -> /usr/bin/php5*

I go back to the bin file, php5 has the * symbol and is green.

-rwxr-xr-x  1 root   root    9049256 Jul  2 11:57 php5*

My PHP script. Very simple. Checks for a cookie and if it exists increments it by one. I then check the results on another page. Manually this works. With crontab, cannot get it to work.

if (!empty($_COOKIE['cronTest'])) {
  $int = $_COOKIE['cronTest'];
  $int++;
  setcookie("cronTest", $int, time()+3600);
}

解决方案

  • Most likely, your script inside /var/www/html/cron is owned by www-data user. Depending on your setup the user executing the cronjob doesn't have permissions to run this file.

  • There are no $_COOKIEs on the command line. A cookie is sent by the users browser. As cli isnt a browser, so you can't read the cookies value. Though you could access a users $_SESSION, but that's another story. Have a look here Is it possible to read cookie/session value while executing PHP5 script through command prompt? for further details.

Your cronjob line looks valid, so the problem will be one of above points. To verify the first, try something without the use of $_COOKIE in your file, like simply

mkdir('/var/www/html/cron/testdir');

just to see if the file can be accessed and a directory is created inside above dir. If it can't be accessed, create a new group and add both the current user ( find out with

ls -al

in /var/www/html/cron ) and the user running the cronjob to the group, then make that group own the file you want to run. See the accepted answer on this question: Set user permissions | Artisan command will not run in code but works fine on command line I posted some time back on how to do that.

For the $_COOKIE problem, you will have to find another solution. For example use Redis or Memcached as a caching service that can be accessed both by online and cli configurations.

Consider to add

1>> /dev/null 2>&1

to the end of your cronjob line, if you don't do so and let cron run this every minute, you will get masses of logfiles.

For the sake of completenes on possible pitfalls when working with php and the cli, allways make sure to provide full paths to your files.

这篇关于使用crontab运行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆