Asked  7 Months ago    Answers:  5   Viewed   43 times

How can I set cron job through PHP script.



This will add a script that runs every day at 9:30am.

exec('echo -e "`crontab -l`n30 9 * * * /path/to/script" | crontab -');

You may run into problems with permissions if you are running this script from a web server. To get around this, I would suggest a different approach.

Here is one possible solution. Create a list of scripts that need to be run. You can save this in a text file or in a database. Create a script to read this list and run it every minute or every 5 minutes (using a cronjob). Your script will need to be smart enough to decide when to run the list of scripts and when to simply exit.

Wednesday, March 31, 2021
answered 7 Months ago

You could use this class PHP-Parse-cron-strings-and-compute-schedules

It'll also compute the last scheduled run

Wednesday, March 31, 2021
answered 7 Months ago

The cron syntax is basically ok.

Try a...

whereis php


whereis php5 find where your php bin folder is located.

Saturday, May 29, 2021
answered 5 Months ago

Cronjob is not something to create as Php process or script. Cron is a linux program that allows you to call a script at a regular interval.

You can see what is an crontab by entering in your linux machine as an admin user and type:

root@valugi:~# crontab -e

You will see something like

*/1 * * * * /usr/bin/php /var/www/somesite/public/cron.php

This means that each minute I am executing the cron.php.

Now, you may want to have different scripts executed at different times and want to pass this logic to php level instead of linux level. If this is the case you may want to call your cron script at the lowest time denominator (minute for example) and in the cron.php build some logic that will call at different times other scripts.

I use for example a Cronable interface:

interface Cronable {
    public function cron();

And each class that wants to be called by the cron.php has to implement this interface and the cron() function, which will specify what is the specific frequency of the call. The cron.php will get all this classes and will compare current time with that time and will decide to execute the call or not.

Tuesday, July 20, 2021
answered 3 Months ago

The Job DSL reference says that timeout takes a closure with absolute() for this case:

job {
  wrappers {
    timeout {
      absolute(minutes = 10)

You can omit the minutes = prefix, but I find it's better to leave it explicit as to what the unit of time is.

Thursday, September 30, 2021
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :