crontab任务不生效

新建php脚本ctTest.php,代码如下:

<?php
/***************************************************************************
 * 
 * Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @file ctTest.php
 * @author sunlihua(com@baidu.com)
 * @date 2018/11/07 11:15:27
 * @brief 
 *  
 **/

$file = fopen("abc.txt", "a+");
$txt = "test\n";
fwrite($file, $txt);
fclose($file);




/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
?>
View Code

 

cron中添加如下定时任务:(crontab -e)

* * * * * /home/users/***/odphb/php/bin/php /home/users/***/odphb/test/ctTest.php

 

 

但是,在/***/odphb/test/目录下始终没有看到abc.txt文件

 

1)怀疑是定时脚本没有执行。

通过sudo tailf /var/log/cron 查看运行日志,有脚本运行日志,如下:

Nov  7 12:06:01 *** crond[41958]: (***) CMD (/home/users/sunlihua/odphb/php/bin/php /home/users/***/odphb/test/ctTest.php)

排除脚本未执行的可能

 

2)怀疑是权限不对造成的,通过sudo crontab -e 添加root角色的定时任务,查看日志,如下:

Nov  7 12:06:01 *** crond[41957]: (root) CMD (/home/users/***/odphb/php/bin/php /home/users/***/odphb/test/ctTest.php)

 

3)在网上看到需要在脚本前加./etc/profile; 查看日志如下:

Nov  7 12:06:01 *** crond[41957]: (root) CMD (./etc/profile;/home/users/***/odphb/php/bin/php /home/users/***/odphb/test/ctTest.php)

 

上面问题都排查了,在/***/odphb/test/目录下还是没有看到abc.txt文件

 

最终原因:cron里面命令用的是绝对路径,脚本里面打开的文件用的也是绝对路径,故文件分别被写到了***和root的根目录下。修改脚本里面abc.txt文件的路径,即修复问题。

posted @ 2018-11-07 12:16  sunflower627  阅读(2787)  评论(0编辑  收藏  举报