简体   繁体   中英

Run a php script with cron in mac

I am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.

So here is what I do:

I wrote this command in a txt file:

    * * * * * /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.

I named the txt file: crontasks.txt and I run it through terminal with this command:

       crontab /Users/dkar/Desktop/crontasks.txt

When I list the crons (crontab -l) I see that this job is listed. 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. It would suffice to run the command crontab -e , which would let you edit your personal crontab or you edit the system crontab.

If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:

  * * * * * /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. Additionally the system crontab has one more field for the username. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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