douhui9631 2013-11-04 13:41
浏览 28
已采纳

如何在zendframework 2中使用cron作业? [重复]

This question already has an answer here:

I have two functions in zend framework 2, which fetch me the details of apartments from another site and save them in my data base. (BUT THEY DO SO ONLY WHEN I RUN THE FUNCTIONS).

say the controller name is 'FetchFromSourceController' and the functions are 'index' and 'ical'. those are the names I used.

But my requirement is those two functions should run automatically one after the other when the day begins, with a gap of 10 minutes. i.e. index action should run at 12:00 AM and ical Action should run at 12:10 AM everyday.

I heard that I could use 'cron job' for that purpose, but never used that before. Please tell me where to write the cron job code in ZF2 to run those functions in those particular times.

or atleast give me some tutorial for using cron job in zendframework2. I searched for it.but not find any. Thanks in advance.

</div>
  • 写回答

3条回答 默认 最新

  • dongwu8050 2013-11-04 19:07
    关注

    ZF2 doesn't have a "cron job" service to automatically run scripts at a specific time.

    A Cron Job is a task performed at a certain time and is typically setup at the operating system level. In unix, it's accomplished via the crontab, in windows you can use the task scheduler. There is nothing in ZF2 which will run your scripts at exactly 12:00 and 12:10.

    Option #1: Cron Job via Admin Panel

    For most hosting environments, you can setup a "cron job" via your website hosting admin panel (such as Plesk or cPanel). There you'll be able to schedule a job by entering in the time and the command to be run. When you create a cron job this way, keep in mind that you'll be entering a command to be run via the command line. Usually this is directly calling php to execute a specific script, but you could also use wget to fetch any URL from the internet. If you directly call php, you'll need to setup a console route as per Zdenek's answer.

    /usr/bin/php /path/to/zf2/public/index.php myIndexConsoleRoute
    

    or

    wget http://yoursite.com/fetch-from-source/index
    

    Option #2: Setup Crontab via command line

    Alternatively, if you have command line access to your server and the proper user permissions, you can setup a crontab via the command line per Zdenek's answer. There is also relevant info here. This is typically the preferred method, but in my opinion it's a bit more involved and can be a challenge to setup and debug, especially if you're not experienced with Unix.

    Option #3: Cron Job service If you're looking for a simpler solution, you might consider one of the cron services out there which will load a specific URL at a given time.

    For example, I've used this service in the past: https://www.setcronjob.com

    After you create your account, you simply enter a URL and select the time you'd like the URL to be called. You could easily accomplish your requirements to call the indexAction at 12:00 and icalAction at 12:10 by setting up two jobs, one to run at 12:00 and the other to run at 12:10. SetCronjob has a free plan which might fit your needs, but be aware that they require you to enter in a captcha every 30 days to renew your free account (which is fine, but becomes annoying after awhile).

    URL vs Command Line

    With "wget" in Option #1 and a Cron Service in Option #3, you'll need to make your indexAction and icalAction available via http. This could be a significant consideration. Even if you add some sort of validation (by checking for a code you send via query string or post), it's still possible that anyone on the internet could trigger these functions if they know the correct 'secret' URL. Having your ical and index methods accessed via http has the additional benefit of you being able to easily call them if you want to manually run these scripts to force an update.

    With the CLI approach mentioned by Zdenek, you create a console route in ZF2 and the only way to trigger that route is via the command line. This is typically preferred because your index and ical methods won't be exposed to http, but it's not always an option depending on your hosting setup, and it might not be a significant concern depending on what these functions do to your data and public website.

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

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试