I am not sure this is the way to do it, but this is working for me.
https://loopback.io/doc/en/lb4/Booting-an-Application.html#bootcomponent
Start with creating a component inside the project folder. I created src\components\cron.component.ts
import { Component } from "@loopback/core";
import { CronJob, CronCommand } from "cron"
export class CronJobsComponent implements Component {
private cj: CronJob;
constructor(){
this.start()
}
async start(){
this.cj = new CronJob('* * * * * *', this.showMessage)
this.cj.start();
}
showMessage:CronCommand = async () => {
console.log("inside cron jobs")
}
}
Next import our component in the application.ts
file
import { CronJobsComponent } from './components'
and register our new component inside the constructor
this.component(CronJobsComponent);
The corn job starts on application boot.
I used https://www.npmjs.com/package/cron and https://www.npmjs.com/package/@types/cron
Hope this helps you.