douyin2962 2014-01-29 02:31
浏览 843
已采纳

在Crontab中使用相对路径

I am attempting to use relative paths in my crontab file on CentOS 6.4, so that I do not have to repeat the same absolute path over and over again. At the top of my crontab file, located here: /etc/crontab, I have:

SHELL=/bin/bash
PATH=/var/www/html/crons
MAILTO=""
HOME=/

And each of my commands looks like:

*/2 * * * * root /usr/bin/php "cronfile.php" >> "logs/cronfile_"`date +\%Y\%m\%d`".log"

I'm expecting that it'll run the cronfile.php PHP file in the /var/www/html/crons directory, and save the output from this to /var/www/html/crons/logs/cronfile.log. However, the file is not being run and the log file is not being created.

The command works fine if I run just:

/usr/bin/php "cronfile.php" >> "logs/cronfile_"`date +\%Y\%m\%d`".log"

from the command line after cding into the /var/www/html/crons directory.

Please advise, thanks.

  • 写回答

2条回答 默认 最新

  • doulai8405 2014-01-29 21:44
    关注

    After many trials and research, I discovered that the solution was using the HOME= variable, not the PATH= variable, like so:

    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=""
    HOME=/var/www/html/crons
    

    And then each of the lines would just look like:

    */2 * * * * root /usr/bin/php cronfile.php >> logs/cronfile_`date +\%Y\%m\%d`.log
    

    Hope this helps someone else with the same issue I had in the future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到