I’m working on a symfony project and I need to execute a task only once at a specific time(for example at 23/06/2022 18:30)
What’s the best way to do that
Is using the crontab a good idea in this case ?
if I understand correctly you want to run an action only once, it's best to do a command and run it manually, if you want to schedule it to run automatically, you can use cron for this purpose, e.g. 30 18 23 06 * php path_of_your_project/bin/console your:command
Related
I have a use case where i need to do certain database manipulations automatically every month on a specific date.Currently using Symfony 2.7 framework is it possible to call a controller every month on a specific date??.Any feedback's/ideas would be helpful
Instead of calling a controller, you could probably create a Symfony Console Command. Have a look here at how to do it: https://symfony.com/doc/2.7/console.html
With this option, the only way to execute it would be via the cli.
Then you can call the command from a cron job that runs at the time you want. Some examples of how to schedule jobs using crontab: https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/
You can make a Console Command in Symfony and call this command every month with a cron task.
Console command
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Currently I'm trying to build a good scheduler system as an interface for setting and editing cron jobs on my system. My system is built using Zend framework 1.11.11 on a Linux server.
I have 2 main problems that I want your suggestion for:
Problem 1: The setup of the application itself
I have 2 ways to run the cron job:
First way is to create a folder scripts and create a common bootstrap file in it where I'll load only the resources that I need. Then for each task I'll create a separate script and in each script I'll include the bootstrap file. Finally, I'll add a cron task in the crontab file for each one of these scripts and the task will be something like ***** php /path/to/scripts/folder/cronScript_1.php .
Secondly treat the cron job like a normal request (no special bootstrap). Add a cron task in the crontab file for each one of these scripts and the task will be something like ***** curl http://www.mydomain.com/module/controller/action .
Problem 2: The interface to the application
Adding a cron job also can be done in 2 ways:
For each task there will be an entry in the crontab file. when I want to add a new task I must do it via cPanel or any other means to edit the crontab (which might not be available).
Store the tasks in the database and provide a UI for interacting with the database (grid to add few tasks and configuration). After that write only 1 cron job in the crontab file that runs every minute. This job will select all jobs from the database and checks if there is a job that should be run now (the time for the tasks will be stored and compared with the current time of the server).
In your opinion which way is better to implement for each part? Is there a ready made solution for this that is better in general??
Note
I came across Quartz will searching for a ready made solution. Is this what I'm looking for or is it something totally different?
Thanks.
Just my opinion, but I personally like both 1 & 2 dependent on what your script is intending to accomplish. For instance, we mostly do 1 with all of our cron entries as it becomes really easy to look at /etc/crontab and see at a glance when things are supposed to run. However, there are times when a script needs to be called every minute because logic within the script will then figure out what to run in that exact minute. (i.e. millions of users that need to be processed continually so you have a formula for what users to do in each minute of the hour)
Also take a look at Gearman (http://gearman.org/). It enables you to have cron scripts running on one machine that then slice up the jobs into smaller bits and farm those bits out to other servers for processing. You have full control over how far you want to take the map/reduce aspect of it. It has helped us immensely and allows us to process thousands of algorithm scripts per minute. If we need more power we just spin up more "workhorse" nodes and Gearman automatically detects and utilizes them.
We currently do everything on the command line and don't use cPanel, Plesk, etc. so I can't attest to what it's like editing the crontab from one of those backends. You may want to consider having one person be the crontab "gatekeeper" on your team. Throw the expected crontab entries into a non web accessible folder in your project code. Then whenever a change to the file is pushed to version control that person is expected to SSH into the appropriate machine and make the changes. I am not sure of your internal structure so this may or may not be feasible, but it's a good idea for developers to be able to see the way(s) that crontab will be executing scripts.
For Problem 2: The interface to the application I've used both methods 1 & 2. I strongly recommend the 2nd one. It will take quite more upfront work creating the database tables and building the UI. In the long run though, it will make it much easier adding new jobs to be run. I build the UI for my current company and it's so easy to use that non-technical people (accountants, warehouse supervisors) are able to go in and create jobs.
Much easier than logging onto the server as root, editing crontab, remembering the patterns and saving. Plus you won't be known as "The crontab guy" who everyone comes to whenever they want to add something to crontab.
As for setting up the application itself, I would have cron call one script and have that script run the rest. That way you only need 1 cron entry. Just be aware that if running the jobs takes a long time, you need to make sure that the script only starts running if there are no other instances running. Otherwise you may end up with the same job running twice.
I am using MVC framework. Now I want to set up cron such that the URL "http://www.xyz.com/controllera/functiona" should be executed. what should i write in the path section for it.
I got something about "GET" command but it wasnt clear.
Can someone please help me out with it?
As you didn't specify any framework the only way to run this cron is this command
wget --spider 'http://www.xyz.com/controllera/functiona'
I assume you are using an MVC framework as controllera is in the url. If it was Kohana (2.3) framework I would have run it by
/usr/bin/php /path/to/index.php controller/method
Most framework has cli interface to run a controller method. Search for your framework.
See these links for different frameworks.
Zend Framework
Kohana 2 & 3
Codeignier
Yii
I don't understand the "module called cron" part of your question. I believe you are confused, cron is a service on Linux and other Unix systems configured thru crontab.
A crontab(5) entry is defined by a time and date and a command to run.
On Linux and Posix systems, you cannot execute or run an URL. Running something involves the execve(2) system call, which requires an executable file path (and arguments).
Perhaps you want to retrieve some URL using the HTTP protocol. You might use a command-line HTTP client, like wget or curl.
So perhaps the command you want to run in your crontab might be
wget http://www.xyz.com/controllera/functiona
but you could use curl
My guess is that you are confused, and don't understand well enough your question. Consider reading some material.
For example, to retrieve that URL once a day at 3 pm, you would have the following crontab entry:
# run everyday at 3 pm a GET HTTP request
0 15 * * * /usr/bin/wget http://www.xyz.com/controllera/functiona
Use the crontab(1) command to configure your crontab (which may contain several entries, and several variable definitions, so you may have to edit it).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: running scheduled jobs (cron jobs)
I need to update a database every 20 mins lets say. (ie. add 50 to 'X' column, subtract 20 form 'y' column, preform an equation based on time on 'z' column, etc.) I have the necessary update in a update.php page but how would i go about calling that page every 20 minutes (short of scheduling a task on a computer)? or is there a better way to do this?
Thanks
Linux
This should be done as a cron job or alternatively using at to schedule the job. I have written a PHP wrapper for the at command that you could use for this purpose: https://github.com/treffynnon/PHP-at-Job-Queue-Wrapper
Windows
You need to use Scheduled Tasks. Here is a link to an old article I wrote about moving Linux cronjobs to a Windows machine: http://blog.simonholywell.com/post/374209271/linux-to-windows-server-migrating-and-securing-your-cron
Where does this page live? If it's on a Linux box, use a cron job. If it's on a Windows machine, use a Scheduled Task.
The answer to your question is no, you're going to have to schedule a task. Basically because this situation is exactly what it is made for!
On Linux you can use a cron job. On windows it is done with a scheduled task
The question is very easy, I want to execute several php files every "N" minutes. For example:
every N minutes
{
execute(script1.php)
execute(script2.php)
execute(script3.php)
}
I know about crontab but i was trying to find another solution. Any suggestions? Thanks in advance.
Using a Cron job is the usual solution. Can you explain why you don't want to use CRON? I've also seen libraries that add cron-like features to your system. For example in the Java/Groovy/Grails world there's the Quartz library/plugin. A quick Google search yielded a PHP library called phpJobScheduler that seems similar to Quartz. I have never used phpJobScheduler so I can't vouch for it.
I'd be interested in why you don't want to use crontabs for this? Are you going to be the primary web operations person running this server or will you be relying on an existing sysop team? You may want to get their input since they are the ones who will be most impacted by what method you choose. I've found they tend to be fond of cron for simple scheduling.
On windows I used built-in proggie called Task Scheduler. As for linux, yes, cron jobs is your answer.
You could create a php script to loop forever do X every N minutes and run the command with a & to make it a background process.
/path/to/php /home/user/bgscript.php &
If you want it to always run, you'd then have to add it to startup init.d or services depending on flavor of *nix.
This solution is possible but personally I would highly recommend going with crontab, its established, proven and works well! Why are you avoiding it?
You could build a script and let it run as a daemon and perform certain tasks on a set interval, but that's actually just simulating cron ... and if you have the ability to run a php script as a daemon you should really also be able to run it as a cronjob since that's what crons are made for.
If you need more info on how to run a php script as a daemon read this great intro. There is also a great comparison between daemon and cron inthere, worth the read.