史京迪
史京迪

Reputation: 481

Cron Job in symfony2

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

Upvotes: 17

Views: 16149

Answers (4)

Thomas Decaux
Thomas Decaux

Reputation: 22671

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.

Upvotes: 0

Cups
Cups

Reputation: 6896

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

Upvotes: 0

ferdynator
ferdynator

Reputation: 6410

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.

Upvotes: 24

vishal
vishal

Reputation: 4083

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

e.g.

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

Upvotes: 2

Related Questions