简体   繁体   English

用mac中的cron运行php脚本

[英]Run a php script with cron in mac

I am trying to run in mac a php script using cron. 我试图使用cron在mac中运行php脚本。 I want this php script to run every one minute. 我希望这个PHP脚本每隔一分钟运行一次。 I read several sources online and from my understanding is better if I use launchd. 我在线阅读了几个来源,如果我使用launchd,我的理解会更好。 In any case, I try to make it work with cron and then if it works fine I might try to use launchd. 在任何情况下,我尝试使它与cron一起工作,然后如果它工作正常我可能会尝试使用launchd。

So here is what I do: 所以这就是我的所作所为:

I wrote this command in a txt file: 我在txt文件中写了这个命令:

    * * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php

There I have the path to php (I found it with the "which php" command) and the path to my php script. 在那里我有php的路径(我发现它与“哪个php”命令)和我的PHP脚本的路径。

I named the txt file: crontasks.txt and I run it through terminal with this command: 我将txt文件命名为:crontasks.txt,然后使用此命令通过终端运行它:

       crontab /Users/dkar/Desktop/crontasks.txt

When I list the crons (crontab -l) I see that this job is listed. 当我列出crons(crontab -l)时,我看到列出了这个作业。 But nothing comes as output every one minute. 但是每一分钟都没有任何输出。 The output is supposed to be an image stored in a specified folder. 输出应该是存储在指定文件夹中的图像。 What am I missing here? 我在这里错过了什么? Thanks in advance D. 提前致谢。

You might have a slight miscomprehension how crond processes the "crontab" files. 你可能会对crond如何处理“crontab”文件有一点误解。 It would suffice to run the command crontab -e , which would let you edit your personal crontab or you edit the system crontab. 只需运行命令crontab -e ,您可以编辑个人crontab或编辑系统crontab。

If you use crontab -e it will open the default editor (vi/vim) and you can enter the line: 如果您使用crontab -e ,它将打开默认编辑器(vi / vim),您可以输入以下行:

  * * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php

Then you simply save your file. 然后你只需保存你的文件。 If you want to use the system crontab you will have to edit it directly and enter the line. 如果要使用系统crontab,则必须直接编辑它并输入行。 Additionally the system crontab has one more field for the username. 此外,系统crontab还有一个用户名字段。 Like this: 像这样:

  * * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php

On the next full minute your script will be executed by crond. 在下一整分钟,你的脚本将由crond执行。

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

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