Cron Job in symfony2

匿名 (未验证) 提交于 2019-12-03 02:45:02

问题:

I create a console command in my project. I want it to be executed everyday at 7 p.m. How could i do it in symfony2? A basic php cron job way or symfony2 have something more convenient? Thanks

回答1:

You can use the basic cron. On debian or ubuntu you can do this:

crontab -e -u <username> 

Where username is the name of the user that should execute the command. In the editor add your command. Here is a good explanation how the line should look. For a Symfony2 command something like this should work:

* * * * * /usr/bin/php /var/www/symfony2/app/console your:command --option=123 

This will execute your:command --option=123 every minute.


On a windows machine you can use the ac command. It is available for windows 7 by default. Read the docs here. It should look something like this:

AT 00:00 /every:M,T,W,Th,F "php /var/www/symfony2/app/console your:command --option=123" 

Make sure that php is available globaly or the path to the php.exe file is correct.



回答2:

You can just set a cron in crontab to execute your symfony command,

e.g.

0 19 * * * /var/www/symfony/app/console YOUR_COMMAND



回答3:

I think this is a good question.

I recently did some work (using ZF1 components, not full MVC) where all kinds of hidden dependencies existed on cron jobs to perform tasks, and I had to spend much time adding comments and creating documentation to warn anyone coming after me what these crons existed, what they did, where they were stored and so on.

It never dawned on me that there would be a ZF component just for that purpose, lots of dependencies on controllers though...

Sorry I'm not answering your direct question though.

FWIW



回答4:

Pay attention to the user you use to execute the command. If you run it as root, root could own Symfony cache then break your site HTTP side (because nginx will run as www-data user for example).

With Docker, I am using phusion base image "setuser" method to force www-data user.



文章来源: Cron Job in symfony2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!