简体   繁体   English

在Mac上使用cron运行php脚本

[英]Running php script using cron on a Mac

What is wrong with this cronjob? 这个cronjob有什么问题?

* * * * * * php -f /Documents/Programs/WeeklyHours/weekly_hour.php

I have combed through the various cron questions on StackExchange and nothing is working. 我在StackExchange上解决了各种cron问题,但没有任何效果。 When I run php -f /Documents/Programs/WeeklyHours/weekly_hour.php in the terminal it works perfectly. 当我在终端中运行php -f /Documents/Programs/WeeklyHours/weekly_hour.php时,它运行良好。 I know the cron jobs are running because I am getting error mail. 我知道cron作业正在运行,因为我收到错误邮件。 At the bottom of the error mail messages it is saying "/bin/sh: Applications: command not found." 错误邮件消息的底部显示“ / bin / sh:应用程序:找不到命令”。 Any ideas on what I am doing wrong? 关于我在做什么错的任何想法吗?

Thanks in advance. 提前致谢。

You have one more * than required in your crontab entry 您的crontab条目比要求的多*

Try 尝试

0-59 * * * * php -f /Documents/Programs/WeeklyHours/weekly_hour.php

The 0-59 is so that it will run every minute 0-59表示它将每分钟运行一次

The cron job likely runs under a different user which does not have its PATH set up the same as you, so it cannot find the php executable. cron作业可能在其他用户下运行,而该用户的PATH设置与您相同,因此找不到php可执行文件。 You are able to simply type php because your PATH variable is set up to include its parent directory; 您可以简单地键入php因为您的PATH变量设置为包括其父目录; that is not necessarily true for all other users. 对于所有其他用户,不一定是正确的。

Explicitly specify the path to the executable, eg /usr/bin/php . 明确指定可执行文件的路径,例如/usr/bin/php To figure out which php you're using, type: 要弄清楚您使用的是哪个php ,请输入:

$ which php
 * * * * * * /usr/local/bin/php -f /Documents/Programs/WeeklyHours/weekly_hour.php >> /ww/xx.log 2>&1

您可以查看日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM