用crontab命令定时PHP文件,并调试

58 篇文章 0 订阅
这是一个用URL定时执行PHP的命令,只要有执行,数据库表里面都会有执行时间的记录,然而crontab -e 添加了如下命令后却发现根本没有执行成功。
30 1 * * * /usr/bin/curl   http://fo.baidu.com/index.php?m=AutoEndOrder&a=autoFinishOrder


调试方法:

1、查看文件是否有可执行权限,我通过URL访问,应该不是这个问题吧

ll
-rw-r--r-- 1 root root 1187 Oct 19 15:35 xinsheng.conf
-rw-r--r-- 1 root root 1088 Jul 28 17:29 ys_erp.conf



上面的文件都是没有执行权限的,如果需要添加执行权限,可以使用命令

chmod +x ./PacketCheckAction.class.php 


2、将执行结果输出到文件中,查看是否有错误


使用CURL访问URL来每5分执行PHP脚本。Curl默认在标准输出显示输出。使用”curl -o”选项,你也可以把脚本的输出转储到临时文件。

*/5 * * * * /usr/bin/curl -o /hotdata/kehu/hdshop/log.txt  http://hdshop.baidu.com/index.php?m=PacketCheck&a=Check&sid=1


查看log.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>系统发生错误</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
html{ overflow-y: scroll; }
body{ background: #fff; font-family: '微软雅黑'; color: #333; font-size: 16px; }
img{ border: 0; }
.error{ padding: 24px 48px; }
.face{ font-size: 100px; font-weight: normal; line-height: 120px; margin-bottom: 12px; }
h1{ font-size: 32px; line-height: 48px; }
.error .content{ padding-top: 10px}
.error .info{ margin-bottom: 12px; }
.error .info .title{ margin-bottom: 3px; }
.error .info .title h3{ color: #000; font-weight: 700; font-size: 16px; }
.error .info .text{ line-height: 24px; }
.copyright{ padding: 12px 48px; color: #999; }
.copyright a{ color: #000; text-decoration: none; }
</style>
</head>
<body>
<div class="error">
<p class="face">:(</p>
<h1>非法操作:index</h1>
<div class="content">
	<div class="info">
		<div class="title">
			<h3>错误位置</h3>  
		</div>
		<div class="text">
			<p>FILE: /hotdata/kehu/hdshop/core/Lib/Core/Action.class.php  LINE: 231</p>
		</div>
	</div>
	<div class="info">
		<div class="title">
			<h3>TRACE</h3>
		</div>
		<div class="text">
			<p>[16-01-14 15:00:01] /hotdata/kehu/hdshop/core/Lib/Core/Action.class.php (231) _404(非法操作:index)<br />
[16-01-14 15:00:01]  () Action->__call(index, )<br />
[16-01-14 15:00:01] /hotdata/kehu/hdshop/core/Lib/Core/App.class.php (187) ReflectionMethod->invokeArgs(, Array)<br />
[16-01-14 15:00:01] /hotdata/kehu/hdshop/core/Lib/Core/App.class.php (207) App::exec()<br />
[16-01-14 15:00:01] /hotdata/kehu/hdshop/core/Lib/Core/Think.class.php (39) App::run()<br />
[16-01-14 15:00:01] /hotdata/kehu/hdshop/core/Common/runtime.php (242) Think::start()<br />
[16-01-14 15:00:01] /hotdata/kehu/hdshop/core/core.php (30) require(/hotdata/kehu/hdshop/core/Common/runtime.php)<br />
[16-01-14 15:00:01] /hotdata/kehu/hdshop/index.php (18) require(/hotdata/kehu/hdshop/core/core.php)<br />
</p>
		</div>
	</div>
</div>
</div>
<div class="copyright">
<p><a title="官方网站" href="http://www.thinkphp.cn">ThinkPHP</a><sup>3.1.2</sup> { Fast & Simple OOP PHP Framework } -- [ WE CAN DO IT JUST THINK ]</p>
</div>
</body>
</html>

果然程序报错了。。、好吧


为什么呢?

1、thinkphp报错,找不到index方法

解决方法:

在要访问的目标文件中,加入一个空的index方法

<?php
/**
 * 红包资格用户轮询
 */
class PacketCheckAction extends Action
{
    public function __construct() 
    {
        parent::__construct();      
    }
    public function  index()
    {
        
    }
添加了index方法后还是不行,直接在Linux上执行显示如下:

[root@iZ94eveq0q4Z sites-enabled]# /usr/bin/curl http://hdshop.baidu.com/?m=PacketCheck&a=Check&sid=1
[3] 17743
[4] 17744

[1]+  Stopped                 php http://hdshop.baidu.com/?m=PacketCheck
[2]   Done                    a=Check

好吧,貌似URL被截断了
2、调用PHP的方式改为pathinfo的方式

[root@iZ94eveq0q4Z sites-enabled]# /usr/bin/curl http://hdshop.baidu.com/index.php/PacketCheck/Check
[root@iZ94eveq0q4Z sites-enabled]# 
果然,没有消息就是好消息。这样就没有问题了,crontab -e 添加如下

*/5 * * * * /usr/bin/curl -o /hotdata/kehu/hdshop/log.txt   http://hdshop.baidu.com/index.php/PacketCheck/Check

crontab使用上可以参考

点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值