dougou2937 2014-03-27 17:25
浏览 81
已采纳

设置Cron作业执行PHP

I have a php page with a text box that accepts a player's id number, makes a request to another page based off that id, using a GET request, and then uses the response.

This part works perfectly. However now I would like to automate the process using a Cron job.

I have never used a Cron job before so I am a bit confused on their execution.

Following this documentation though: [http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/][1

I am attempting to set one up. Firstly, after opening the crontab through crontab -e if I wanted to execute my php page every minute I'd write:

1 0 0 0 1 /updateplayer.php

My first question is, do I have to write a job for each day of the week if i want it to run every day every minute or is there an all days option not listed in this documentation?

Secondly, in order to get the "important" part of that php page to run, I need to have my $_GET['playerid'] set to a certain player id. I'm hoping I might accomplish this through part of the arg1 arg2 portions, (following the phpexecute.php part of my proposed cron job). However this is not working as well as expected. Can anyone help me to get this job running?

*Note that I do understand the job will only work for one playerid that is designated in the job.

  • 写回答

1条回答 默认 最新

  • dongshi3818 2014-03-27 17:41
    关注

    You don't need to write a cron job for every day of the week. Instead of a 0 or 1 in your cron job example you can use an asterisk (*) which will mean all. So an asterisk on the second column (hour) would mean run it every hour. An asterisk on the first column (minute) would run it every minute.

    The cron job would look like this:

    * * * * * php -f /[path to dir]/updateplayer.php [playerid]
    

    For the second part, you can pass arguments to the PHP script although you need to fetch them through the $argv variable instead of $_GET. In the example I provided you would fetch the player id via $argv[1]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?