首页 / PHP / 使用crontab运行PHP脚本
使用crontab运行PHP脚本
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用crontab运行PHP脚本,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2938字,纯文字阅读大概需要5分钟。
内容图文
![使用crontab运行PHP脚本](/upload/InfoBanner/zyjiaocheng/819/8bcef392af2d4828bcecf1afee1495ae.jpg)
我理解这是问题,但无论我查了多少教程,我都无法让我的crontab工作,我正在构建一个网站,它将依赖crontab每晚重置我数据库中的特定设置.
这是我的crontab文件:
# 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
如果我尝试cd到/usr/bin/php我得到
-bash: cd: /usr/bin/php: Not a directory
所以我cd到/usr/bin /这就是我发现的:
-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*
如果我cd到/ etc / alternatives我发现:
lrwxrwxrwx 1 root root 13 Jun 13 09:36 php -> /usr/bin/php5*
我回到bin文件,php5有*符号并且是绿色的.
-rwxr-xr-x 1 root root 9049256 Jul 2 11:57 php5*
我的PHP脚本.非常简单.检查cookie,如果存在,则将其递增1.然后我在另一页上查看结果.手动这是有效的.使用crontab,无法让它工作.
if (!empty($_COOKIE['cronTest'])) {
$int = $_COOKIE['cronTest'];
$int++;
setcookie("cronTest", $int, time()+3600);
}
解决方法:
>最有可能的是,/ var / www / html / cron中的脚本归www-data用户所有.根据您的设置,执行cronjob的用户无权运行此文件.
>命令行中没有$_COOKIE.用户浏览器发送cookie.由于cli不是浏览器,因此您无法读取cookie值.虽然你可以访问用户$_SESSION,但这是另一个故事.有关详细信息,请查看Is it possible to read cookie/session value while executing PHP5 script through command prompt?.
你的cronjob线看起来有效,所以问题将是以上几点之一.要验证第一个,请在您的文件中尝试不使用$_COOKIE,就像简单一样
mkdir('/var/www/html/cron/testdir');
只是为了查看是否可以访问该文件并在目录上创建一个目录.如果无法访问,请创建一个新组并添加当前用户(请查找
ls -al
在/ var / www / html / cron中)和运行cronjob到组的用户,然后使该组拥有您要运行的文件.在这个问题上看到接受的答案:Set user permissions | Artisan command will not run in code but works fine on command line我已经回过头来讨论如何做到这一点.
对于$_COOKIE问题,您必须找到另一种解决方案.例如,使用Redis或Memcached作为缓存服务,可以通过在线和cli配置访问.
考虑添加
1>> /dev/null 2>&1
在你的cronjob行结束时,如果你不这样做并让cron每分钟运行一次,你将得到大量的日志文件.
为了完成使用php和cli时可能出现的陷阱,总是确保提供文件的完整路径.
内容总结
以上是互联网集市为您收集整理的使用crontab运行PHP脚本全部内容,希望文章能够帮你解决使用crontab运行PHP脚本所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。