Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using cron to run scheduled tasks
#1
The scheduled task system in MyBB is used to perform many tasks at scheduled intervals, but requires users to be accessing your board and can reduce load times in some cases.

In order to ensure tasks are ran in a more robust and reliable way, it can be beneficial to run either all or critical tasks using the Cron daemon on your server. Cron is described as follows:

Wikipedia Wrote:The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like connecting to the Internet and downloading email at regular intervals.
Luckily, MyBB has support for running individual tasks via Cron or to run all tasks on an individual basis based upon their calculated next run time.

Getting the ID of a task

In order to run a single task via Cron, you need to find out the ID of the task you wish to run. This can be found via the MyBB Admin Control Panel, under the Tools & Maintenance > Task Manager section. Hovering over a task title or clicking a task title will link you to the task edit page. Hovering the link will show the URL in your browser's status bar, as shown below.

   

In the above screenshot, the ID of the "Tables Check" task is 7.

Creating a Cron job to run the "Tables Check" task every 24 hours

In order to run the "tables check" task once every 24 hours, we can use the following Cron job:

0 0 * * * /usr/bin/php /var/www/myforum/task.php 7 > /dev/null 2>&1

The above will run the task at midnight every night. Note that you will have to change "/var/www/myforum/" to be the real path to your forum install on your server. You may also have to change the path to PHP from "/usr/bin/php" to wherever PHP is installed on your server.

To change the task to be ran, simply change "7" to the ID of the task to run, which can be retrieved as described above.

Also note that some server control panels such as cPanel have user interfaces to manage cron jobs which make this process much easier.

The creation of a cron job or the syntax of a job is outside the scope of this guide, but there are many tutorials on the internet depending upon your host, operating system and setup.

Creating a Cron job to run the next scheduled task every 5 minutes

In some cases, you may wish to simply run whichever task is scheduled next. This can be done using the built in scheduling in MyBB, by simply executing the "task.php" script via Cron. This way, tasks will run almost on schedule with the way they are configured in the MyBB ACP. The Cron job to do so is as follows:

*/5 * * * * /usr/bin/php /var/www/myforum/task.php > /dev/null 2>&1

Note that you will have to change "/var/www/myforum/" to be the real path to your forum install on your server. You may also have to change the path to PHP from "/usr/bin/php" to wherever PHP is installed on your server.

Removing the normal task image

If you're running tasks via Cron, you generally do not want to run them using the usual MyBB method (which utilises a small invisible image at the bottom of every page to run the next scheduled task). This can be disabled by editing the footer template for your theme. You can do this via ACP > Templates & Style > Templates > *YOUR TEMPLATE SET* > Footer Templates > footer. In this template, find and remove the following code before saving the template:

<!-- The following piece of code allows MyBB to run scheduled tasks. DO NOT REMOVE -->{$task_image}<!-- End task image code -->
Reply
#2
This is really informative post, adding "> /dev/null 2>&1" to the line end of line can suppress messages there, as a good practice, isn't it?
My current MyBB project: Ops FAQs | OpsFaqs.com 
Reply
#3
(05-15-2015, 04:51 PM)_Jerry_ Wrote: This is really informative post, adding "> /dev/null 2>&1" to the line end of line can suppress messages there, as a good practice, isn't it?

Yes, that is useful. I'll add that.
Reply
#4
might not hurt to add this to your OP so users can find out where php is running from. if they have root access, they can run this command in *nix: which php

few questions I had;

1- I suppose if I wanted my tasks to run at different times, I'd need to add a new line for each task in my crontab, eh?

2- if I move all of my tasks over to a crontab, I'm guessing I'd need to a) remove that hidden image as explained and b) disable the tasks within the task manager?

3- does running a task via cron still add the run times to the task log?
Reply
#5
1) Yes, you would need separate entries for every task in that case.
2) Just removing the image will suffice. There's no need to disable them. Doing so will in fact stop them from running.
3) yes, that is handled by the task.php file, which the Cron job executes Smile
Reply
#6
For some reason for me when I add this to my crontab it will only run 1 task every time cron runs the command? For example I have 2 tasks set to run every minute but it will only run 1 of them if I change to cron. I even tried to add more lines to crontab and make it sleep for 10 seconds before running the extra line but it will still only run 1 task per minute. Why is this? I would like to run all tasks that are set to run instead of only 1.

Not to mention information provided here conflicts information here; https://docs.mybb.com/1.6/Help-Task-System/ specially for the part where it says you should disable tasks on admincp and here Euan stated that you should leave them enabled.
Reply
#7
As Euan T said: "In some cases, you may wish to simply run whichever task is scheduled next".
If you want to launch specific task, you have to make multiple calls to task.php:
* * * * * /usr/bin/php /var/www/myforum/task.php 8 > /dev/null 2>&1
* * * * * /usr/bin/php /var/www/myforum/task.php 11 > /dev/null 2>&1

Another small solution:
Create a copy of multitask.php in the root directory of your board and then use the following cron:
* * * * * /usr/bin/php /var/www/myforum/multitask.php 8,11 > /dev/null 2>&1
Take care: no space between comma and tasks id.

Source of multitask.php (simple variant of task.php):
<?php

/**
 * Multitask execution
 * usage : php /home/you/mybb/multitask.php tid1,tid2,tid3
 */
ignore_user_abort(true);
@set_time_limit(0);

define("IN_MYBB", 1);
define("NO_ONLINE", 1);
define("IN_TASK", 1);
define('THIS_SCRIPT', 'multitask.php');

require_once dirname(__FILE__) . "/inc/init.php";

// Load language
$lang->set_language($mybb->settings['bblanguage']);
$lang->load("global");
$lang->load("messages");

if (function_exists('mb_internal_encoding') && !empty($lang->settings['charset'])) {
    @mb_internal_encoding($lang->settings['charset']);
}

require_once MYBB_ROOT . "inc/functions_task.php";

// Are tasks set to run via cron instead & are we accessing this file via the CLI?
// php task.php [tid]
if (PHP_SAPI == "cli") {
    // Passing a specific task ID
    if ($_SERVER['argc'] == 2) {
        $tids = explode(',', $_SERVER['argv'][1]);
        foreach ($tids as $ttid) {
            $query = $db->simple_select("tasks", "tid", "tid='" . (int) $ttid . "'");
            $tid = $db->fetch_field($query, "tid");
            if ($tid) {
                run_task($tid);
            } else {
                run_task();
            }
        }
    }
}
This script is built to work only in CLI mode.
[MyBB 1.8 mods] - Offer me a coffee
Do not ask me help through PM
Reply
#8
Yeah, the standard MyBB task system only does one task at a time, which makes sense when doing it as part of a request (and timeouts are a problem).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)