Search code examples
phpmysqlcron

how to start customized and automated cron


I'm creating a site in php, which analyzes instagram profiles such as number of followers, likes etc. now I want to create a sort of grid where the number of followers are stored every day, so as to have a grid with the number of daily followers at the end of the month I thought of creating a cron with a script that saves me the number of followers every day on a database, but obviously I cannot save the data of all instagram users on the database, but to save them only when the user in question is searched , I don't know if I explained myself well so I give an example:

I look for the username "john35" on my site FOR THE FIRST TIME.

john35's data is shown to me and an empty grid is shown to me because there is no data in the database. from this moment a personalized cron for john35 starts, which saves the number of followers every day

it's possible to do? and how?


Solution

  • I don't know how other used to do it, but I always use supervisor for these type of jobs. It's similar to the cron, but there are much more configuration options.

    Anyway, example for installation(CentOS) and implementation with it:


    Create a service file, and fill it with these lines:

    [Unit]
    Description=Process Monitoring and Control Daemon
    After=rc-local.service nss-user-lookup.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
    
    [Install]
    WantedBy=multi-user.target
    

    then enable the auto start:

    sudo systemctl enable supervisord
    

    and start the service:

    sudo systemctl start supervisord
    

    [Note] You have to start supervisord before you can access to supervisorctl.

    sudo supervisord -c /etc/supervisord.conf
    sudo supervisorctl -c /etc/supervisord.conf
    

    After this point you can use it easily:

    // Reload the daemon’s configuration files, without add/remove (no restarts)
    sudo supervisorctl reread
    
    // Reload config and add/remove as necessary, and will restart affected programs
    sudo supervisorctl update
    
    
    sudo supervisorctl <start/restart/stop> <workerIniFileName>:*
    

    [Note] Ini file location: /etc/supervisor.d/

    Example: random-worker.ini

    [program:random-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=<php/sh/py/command/ect script location> // example: php /var/www/html/project/asd.php
    autostart=true
    autorestart=true
    user=apache // or any other user
    numprocs=4 // number of process you would like to execute simultaneously
    redirect_stderr=true
    stdout_logfile=/var/log/<name of the logfile>.log
    

    The script implementation is totally up to you. Example in php:

    public function onShutdown()
    {
        $this->info('Shutdown called, exiting.');
    }
    
    public function onSignal($signo)
    {
        $this->info("Received PCNTL signal {$signo}");
        die();
    }
    
    public function actionRunWorker($priorityLevel = false)
    {
        set_time_limit(0);
        declare(ticks=1);
    
        register_shutdown_function([$this, 'onShutdown']);
        pcntl_signal(SIGTERM, [$this, 'onSignal']);
        pcntl_signal(SIGHUP, [$this, 'onSignal']);
        pcntl_signal(SIGINT, [$this, 'onSignal']);
    
        sleep(self::WORKER_SLEEP_TIME);
        while (1) {
            // Your code comes here
            sleep(self::WORKER_SLEEP_TIME);
        }
    }