crontab任务不生效

新建php脚本ctTest.php,代码以下: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)vim

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

 

 

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

 

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

经过sudo tailf /var/log/cron 查看运行日志,有脚本运行日志,以下:code

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

排除脚本未执行的可能crontab

 

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

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

 

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文件的路径,即修复问题。

相关文章
相关标签/搜索