How to Manage Cron Jobs with TYPO3 Scheduler?

Are you looking to get knowledge of the TYPO3 scheduler? You landed at the right place; In this blog post, you will find beginner to advanced level skills on typo3 scheduler tasks, Symfony console commands, and TYPO3 cronjobs, etc.

How to Manage Cron Jobs with TYPO3 Scheduler?

Are you looking to get knowledge of the TYPO3 scheduler? You landed at the right place; In this blog post, you will find beginner to advanced level skills on TYPO3 Scheduler tasks, Symfony console commands, and TYPO3 cron jobs, etc.

TYPO3 has a built-in scheduler or planner feature for 20+ years - genuinely proud to be part of such a fantastic TYPO3 CMS. Fortunately, it’s so easy to configure and use the TYPO3 scheduler to automate tasks. As far as I know, no other Opensource CMS has such a schedule within the core.

And… My dear TYPO3 reader, as my start-up ritual, I want to dedicate this blog to initial developer Christian, Markus, the TYPO3 core team, and the community that developed and maintained such an excellent TYPO3 scheduler feature. #T3Kudos

Since TYPO3 v9, the TYPO3 scheduler task is more potent because TYPO3 collaborated with the Symfony framework by using a fantastic Symfony console command component. That’s one of the great examples, what can be achieved when two giant Open Source frameworks meet & collaborate.

If you are a newbie, TYPO3 Scheduler is one of the core TYPO3 extensions used to execute and perform recurring tasks. Here is the example of some of the tasks;

  • Send a newsletter to your customers.
  • Delete unnecessary images and assets
  • Remove caching and garbage collection
  • Regularly optimize MySQL database
  • Keep removing old-logs
  • etc.,

The Scheduler derives from extension Gabriel, initially developed by Christian Jul Jensen and further developed by Markus Friedrich.

By default, the TYPO3 scheduler extension is available within your built-in TYPO3 core. Sometimes your administrator may not include the TYPO3 scheduler extension, then you can easily install and configure it below.

Composer TYPO3 Scheduler Installation

 

composer require typo3/cms-scheduler

Without-Composer TYPO3 Scheduler Installation

For your standard TYPO3 installation, you should have a built-in scheduler extension; just activate it ;)

Activate TYPO3 Scheduler Extension

Go to Admin Tools > Extensions > Activate “scheduler” extension.

Configuration of TYPO3 Scheduler

Go to Admin Tools > Settings > Extensions Configuration > scheduler

Before we start working with the TYPO3 scheduler, let’s test-drive if everything works well with our TYPO3 scheduler service.

Go to System > Scheduler > Choose “Setup Check”. If you found anything wrong, then contact your system administrator to correct it.

As a TYPO3 administrator, you should know how-to manage TYPO3 schedulers, tasks, execution, TYPO3 cronjob, etc. Let’s have a quick look at the test-drive TYPO3 scheduler task.

Create a TYPO3 scheduler task

Step 1. Go to System > Scheduler > Choose “Information” from the dropdown.

Step 2. Click on the “+” button of the “Scheduler test task.”

Step 3. Fill-up all necessary fields like type, frequency, email, etc.

Step 4. Click on the “Save & Close” button.

Manually Execute/Run TYPO3 Scheduler Task

Step 1. Go to System > Scheduler > Scheduled Tasks

Step 2. Click on the “Run Task” play button.

Step 3. TYPO3 Scheduler should successfully run. Now let’s time check email ;)

Woohoo! We got our TYPO3 scheduler test task’s email, and It means our TYPO3 scheduler works well.

Manually Stop TYPO3 Scheduler Task

Step 1. Go to System > Scheduler > Scheduled Tasks
Step 2. Click on the “Stop Scheduler” button.

Organize TYPO3 Scheduler Tasks Group

Whenever you have many TYPO3 scheduler tasks, TYPO3 has an innovative feature to organize everything in various groups.

It’s effortless; You will just need to add and assign “Task Group” to each TYPO3 scheduler task.

Well, you got the backend management of the TYPO3 scheduler, but that does not end with the TYPO3 scheduler ;) Many people think TYPO3 will handle the recurring next cron, but that’s not true.

Basics TYPO3 Scheduler Command on CLI

You will need to configure TYPO3 Cronjob at your server to execute the recurring TYPO3 scheduler task. First of all, let’s learn the basics, how TYPO3 runs on CLI (command line interface).

 

// Composer based TYPO3 Setup
php vendor/bin/typo3 scheduler:run

// Without Composer Setup
php typo3/sysext/core/bin/typo3 scheduler:run

// Some server don’t directly run PHP, you should find out PHP CLI-path
which php
/usr/local/bin/php typo3/sysext/core/bin/typo3 scheduler:run

Get Help for TYPO3 Scheduler Command

 

// Run this command to Get help of TYPO3 scheduler
vendor/bin/typo3 scheduler:run --help

Run Single Task on TYPO3 Scheduler

 

// Run this command to run single task 
vendor/bin/typo3 scheduler:run --task=1

 

Run Multiple Task on TYPO3 Scheduler

 

// Run this command to run single task 
vendor/bin/typo3 scheduler:run --task=1 --task=2

 

Force Run Task on TYPO3 Scheduler

 

// Forcefully Run this command to run single task 
vendor/bin/typo3 scheduler:run --task=1 -f

 

Debug Task on TYPO3 Scheduler (Recommend)

 

// Forcefully Run this command to run single task 
vendor/bin/typo3 scheduler:run --task=1 -v

I’m sure you are well aware of servers like Apache/Nginx crontab/cronjob. We need to connect the TYPO3 scheduler with such cronjob for our recurring TYPO3 scheduler task.

 

// On Unix and Mac OS X

crontab -e
*/15 * * * * /usr/local/bin/php /www/typo3/sysext/core/bin/typo3 scheduler:run

// Another way - make sure to add “www” user
/etc/crontab and /etc/cron.d/\*
*/15 * * * * www /usr/local/bin/php /www/typo3/sysext/core/bin/typo3 scheduler:run

// On Windows
schtasks /create /sc minute /mo 15 /tn "T3scheduler" /tr "c:\winstaller\php\php.exe c:\winstaller\htdocs\quickstart\typo3\sysext\core\bin\typo3 scheduler:run"

 

Do you know known-problem?

Currently, the main problem is that a running task cannot be killed because no relation exists to the (cron) process that is running the Scheduler. The process PID could be retrieved, but that may not work on all platforms. And can the process be killed afterward? Anyway, it may not be safe to do that.

For your better optimized TYPO3 installation, I recommend configuring below the default TYPO3 scheduler, which will keep neat and clean your TYPO3 instance.

  • Caching framework garbage collection (scheduler)
  • Fileadmin garbage collection (scheduler)
  • Table garbage collection (scheduler - sys_log)
  • Table garbage collection (scheduler - sys_history)

TYPO3 uses Symfony commands API for writing CLI (command line interface) commands. These commands can also be run from the TYPO3 scheduler.

Are you looking for a step-by-step guide to create a custom TYPO3 scheduler task and command? Here we go?

Step 1. Register Symfony Console Commands

Create typo3conf/ext/yourextension/Configuration/Services.yaml

 

services:
 _defaults:
   autowire: true
   autoconfigure: true
   public: false


 NITSAN\NsBasetheme\:
   resource: "../Classes/*"

 Vendor\Extension\Command\TestScheduler:
   tags:
     - name: "console.command"
       command: "myext:test"
       description: "An example description for a command"
       # not required, defaults to false
       hidden: false

 

Step 2. Create Scheduler Command Controller Class

Create typo3conf/exte/yourextension/Classes/Command/TestScheduler.php

 

<?php
namespace Vendor\Extension\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class TestScheduler extends Command
{
   /**
   * Configure the command by defining the name, options and arguments
   */
   protected function configure()
   {
       $this
           ->setDescription('Test Scheduler')
           ->setHelp('Description of the test scheduler')
           ->addArgument('id', InputArgument::OPTIONAL, 'id');
   }
   /**
    * Executes the command for showing sys_log entries
    *
    * @param InputInterface $input
    * @param OutputInterface $output
    * @return int error code
    */
   protected function execute(InputInterface $input, OutputInterface $output)
   {
       $io = new SymfonyStyle($input, $output);
       $io->writeln('Initiated and Processing...');
       $id = $input->getArgument('id');
       if(!$id) {
           $io->error('No id provided');
       }
       else {
           $io->success('Successfully run id='.$id);
       }
   }
}

 

Step 3. Create a TYPO3 Scheduler Task

Go to System > Scheduler > Add New Task > Choose Class = Execute console commands

Choose “myext:test TYPO3 Scheduler” at Scheduler Command.

Step 4. Let’s Have Test-Drive with CLI-Commands

 

// For composer-based TYPO3 instance, I suggest to dump-autoload
composer dump-autoload

// Syntax
vendor/bin/typo3 myext:test [id]

// Without passing argument
vendor/bin/typo3 myext:test

// Without passing argument
vendor/bin/typo3 myext:test id=1

User Help on our TYPO3 Scheduler Command

 

// Display help
vendor/bin/typo3 myext:test -h

Set Cron to TYPO3 Scheduler

 

// Let’s schedule with Crontab

crontab -e
*/15 * * * * /usr/local/bin/php /www/typo3/sysext/core/bin/typo3 myext:test

Thanks for reading my article. I hope you enjoyed and learned well about the TYPO3 scheduler.

As TYPO3 administrator, keep learning scheduler tasks management, cronjob setup, etc. Whenever you want to make a custom scheduler for your specific needs, the TYPO3 developer can help you create a TYPO3 scheduler task with Symfony console commands.

Are you facing any difficulties to set up the TYPO3 scheduler? Do you have any questions? Feel free to write to the comment box.

Have a Happy TYPO3 Scheduling!

Post a Comment

×
Captcha Code Can't read the image? Click here to refresh
  • user
    Archer Dowling 2023-02-06 at 1:17 am
    To the t3planet.com webmaster, Your posts are always well structured and easy to follow.
  • user
    Eric Weisz 2021-04-13 at 7:29 am
    Yeah! TYPO3 Scheduler is one of the best features to manage Cron jobs. Excellent article with excellent explanation.

You May Also Like