With a fair few extensions now tapping into the scheduler, I thought it useful to put together a guide on how to get that up and running. I'm not going to go into the details of cron itself, but if you fancy some extra reading, I'd recommend checking out this wikipedia article about cron.

Why should I care?

Quite simply, a growing list of extensions now support handling certain functions automatically for you, completely behind the scenes. Wondering why fof/drafts 'scheduled drafts' are not posting, or fof/best-answer 'remind users to set a best answer after X days' does not fire? That'll be because they will setup themselves with the scheduler service, but without a one-liner cron job, nothing will happen!

What extensions currently use the scheduler?

Know of any more? Let me know and I'll add them to this list!

Ok, let's get this setup!

Easy! Most (if not all) linux distros either come with, or can have cron installed. For example, on Debian and Ubuntu based systems, install cron like this:

$ sudo apt-get update
$ sudo apt-get install cron

Once you have cron installed, let's create the one and only entry you need for Flarum:

$ crontab -e

This will open the cron editor. You may or may not have other entries there. Add this line, and remember to leave an empty line at the bottom!

* * * * * cd /path-to-your-project && php flarum schedule:run >> /dev/null 2>&1

You may need to experiment with the path to php, etc

* * * * * tells cron to run your command every minute

cd /path-to-your-project && php flarum schedule:run simply says php flarum ...... you've likely seen this many times before!

>> /dev/null 2>&1 simply suppresses any output from the command (you won't need this anyway)

Voila! Now any extension that registers a task to run, anything from every minute to daily, monthly, yearly - whatever - will now run on your server.

This post will likely be a work-in-progress for a while! Feel free to contribute anything you feel relevant!

    • [deleted]

    IanM thanks. Definitely worth remembering when I finally get around to converting my RSS to post script into an extension. My current script relies on a Cron task to process periodically.

    Hi, i have cron available directly from the cpanel.
    My forum is small so I should be using runtime mode for for/sitemap.
    How do you recommend setting up the cron? 1 time a day?
    If yes, what would be the command to send to add the cron process?
    Thanks so much.

    https://ibb.co/Fnty5Ts

      Gatsu The cron command that runs php flarum schedule:run should always run. That command does not determine how often the scheduled tasks run - it simply checks if a scheduled task should be run.

      For fof/sitemap, the cron job is the same. You just change the interval in the settings modal of the extension.

        datitisev Sorry but I did not understand how to proceed.
        I have the possibility to use the cron via the cPanel, but the problem is that I have not understood what I have to write in command.
        I understand that the command is generic, so just indicate "flarum" and not the extensions individually.
        What I didn't understand is: what is the command to send via cPanel? (I have attached the screenshot above)
        I would like to start the cron 1 time per day.
        Thanks for your patience and availability.

          Gatsu I would like to start the cron 1 time per day.

          As in the OP, the cron should run * * * * * - every minute, every day. The command is listed.

          * * * * * cd /path-to-your-project && php flarum schedule:run >> /dev/null 2>&1

          then set the frequency from the settings modal

            IanM Thank you. I tried this but I got this error

            -bash: CHANGELOG.md: command not found

              Gatsu The * isn't part of the command. That's the frequency of the command. The command itself should only contain cd /path-to-your-project && php flarum schedule:run >> /dev/null 2>&1.

                I have vesta control panel similar to cpanel running on my vps
                Do I still need to install cron? I think cron is already installed and running on my server as I can see it in my panel

                So in this case should I just add this line?
                php flarum schedule:run >> /dev/null 2>&1

                  HD3D That looks like cron, yeah. You'll need the cd part as well so the command is executed in the proper directory.

                    datitisev What I can't understand is.. Do I need to run cd command from my vesta control panel or from SSH?

                    And do I need to run $ crontab -e command first or simply run cd /path-to-your-project && php flarum schedule:run >> /dev/null 2>&1 ?

                      HD3D crontab -e opens the list of scheduled cron jobs in an editor. What you are using is an alternative to that.

                      The cd command needs to be included in that command - not run through SSH. That tells it to use the flarum file in your website's root, otherwise I don't know what directory it'd run it in.

                        datitisev Ok, I think I have setup the cron job
                        I'm getting this message when I run php flarum schedule:run
                        Running scheduled command: '/usr/bin/php7.2' 'flarum' drafts:publish > '/dev/null' 2>&1

                        Does it mean my cron for flarum is running correctly?

                          HD3D I think so, yeah. When you say "when I run ...", are you running it manually or did you mean the output from the cron job? There's no point in running it manually as it constantly runs with the cron job.

                          The cron job and the command are separate. The cron job simply executes the command at the specified intervals. The command that is being run, in this case php flarum schedule:run does not know of said interval.

                            Gatsu yes it's normal. schedule:run runs every minute. Everytime it runs, it checks if a command has been scheduled for that particular minute. Most of the time there will be no commands ready to run.

                            If there's a command scheduled say, every five minutes, by Flarum, 4 times out of 5 you'll see " No scheduled commands are ready to run." and 1 time out of 5 you'll see "running command [...]"

                            9 months later

                            How does the scheduler behave if it's missed? Lets say I want to run it every 30 minutes as opposed to every minute, is it doable, and will the jobs queue up and run together on the next cron execution or does the scheduler treat them as being past and assume they were executed?

                              ctml as far as I know, the scheduler itself cannot catch back on missed tasks.

                              You run the scheduler script every minute.

                              Then that script takes care of checking which of the tasks need to run at that particular minute.

                              If you want to change the schedule of a task, you need to do that at the task registration level. Most extensions allow you to choose the task schedule from the UI.

                              If you don't run the scheduler every minute, I'm pretty sure any job that was supposed to run at that exact minute will never run.