Apex Training - Crontab Job

Let's create our crontab job that will execute once every 24 hours, and randomly pick a winner using our pick_inner() method we created in the previous library. Within tyerminal, type:

./apex create cron training:pick_winner

This will create a new file at /src/training/cron/pick_winner.php. Open the file, and enter the following contents.

declare(strict_types = 1);

namespace apex\training\cron;

use apex\app;
use apex\libc\db;
use apex\libc\debug;
use apex\training\lottery;

 * Class the handles the lottery package, and pickcing a 
 * winner every 24 hours.
class pick_winner

    // Properties
    public $time_interval = 'D1';
    public $name = 'Lottery - Pick Winner';

 * Picker lottery winner.
public function process()

    // Get a random user
    $client = app::make(lottery::class);
    $userid = $client->pick_winner();

    // Return
    return $userid;



As you will notice within the properties of the above class, we set $time_interval = 'D1';, which represents one day and is the interval at which the crontab job will execute. That's it, the above method will not execute every 24 hours.


Let's move on to Creating a Notification Controller to handle the e-mail messages that will be sent out.

Need a Professional?

Need development work by the creator of Apex? E-mail matt.dizak@gmail.com for a free consultation.

Recent News

Apex v1.5.4 Released!
Added .env file, Remote Access Client, finalized PostgreSQL, and more...
Apex v1.5 Released!
Auto-Install via YAML, Preloading, CRUD Scaffolding, Code ReOrganization, Import / Export between Packages, and more...
Apex v1.4.0 Released!
New Package Format, Service Providers and Adapters, New Theme Tools, and more

Mailing List

Subscribe to the low traffic mailing list to stay updated on Apex.

Admin Screenshot