How to work use cronjobs with slimphp


#1

Hi all, I have an issue and I would like to know how to best approach this. I need to create some cronjobs on the server for my slimphp api. I have tried to use wget (which I have to use) to access a file at a specific path

i.e. wget -q -O - "https://mysite.com/some/path/cronjob.php"

The folder structure I have is as follows

app/
   public/
   scr/
   v1/
   scripts/
      php/
         admin/
            cronjob.php
   vendor/
app.php

I would like to be able to run that cronjob using wget. How can I navigate my folder path structure via the url to reach that path? I understand I may need to allow /scripts/php/admin/* in the “passthrough” section of the middleware.php file but I tried that and it is not working

i.e.

    $container['JwtAuthentication'] = function ($container) {
        return new JwtAuthentication([
            'path'        => '/',
            'passthrough' => [
                '/scripts/php/admin/*'
            ],
            'secret'      => getenv('JWT_SECRET'),
            'logger'      => $container['logger'],
            'error'       => function ($request, $response, $arguments) {
                return $response
                    ->withStatus(401)
                    ->withHeader('Content-Type', 'application/json');
            },
            'callback' => function ($request, $response, $arguments) use ($container) {
                $container['token']->hydrate($arguments['decoded']);
            }
        ]);
    };

Also am I going about this the wrong way? Should a route be used for this and then I have a class that is essentially going to be responsible for whatever I want that cronjob to do and then use wget to hit that route (while declaring a pass through option)


#2

Calling a cronjob script via a URL can be problematic, because of timeouts etc…

It would be better to add a crontab task and call the php script directly:

Edit crontab file:

crontab -e

Add a crontab command (1x per minute)

* * * * * /usr/bin/php /var/www/some/path/cronjob.php

Crontab – Quick Reference


#3

I agree with @odan that calling a PHP script for doing a cron-like operation is better off being done server-side.

But if you really want to get something PHP-like working, then I suggest PHP Quartz (based on Java Quartz library).

I don’t have experience with the PHP implementation but I have used the Java version and it is quite nice. YMMV.


#4

Hey guys thanks for the reply!

So part of the problem is I am using a service called serverpilot which I used to set up my Digital Ocean droplet and they recommend you use wget for cron jobs because running the file directly may not work

https://serverpilot.io/docs/how-to-use-cron-to-schedule-scripts

This is why I am trying to find a way to access the url


#5

I am not sure if this will help you, but I use “curl” to call cron URLs and it works just fine.


#6

Thanks Lobos,

I just ended up using command line calling php. The script worked when I manually ran it so I will be able to tell next month on the 1st when it runs if it worked. Thanks for the help