Skip to content

Schedule Background Task

matiasdelellis edited this page May 5, 2020 · 2 revisions

Schedule Background Task

The FaceRecognition application is designed to run as a scheduled task. This allows analyze the photos and showing the results to the user progressively.

Of course, before configuring it as a scheduled task, you must ensure that your configuration works correctly. You must decide how often to execute the task, and the maximum time to complete it, ensuring that it is not executed twice. For this example, suppose we will execute the task every 30 minutes, with a duration of 15 minutes.

Cron

After this, the best way use the operating system cron feature is the preferred method for executing regular tasks.

To run a cron job on a *nix system, every 30 minutes, under the default Web server user (www-data , wwwrun, or apache), you must set up the following cron job to call the occ face:background_job -t 900 command:

# crontab -u www-data -e

And append this line:

*/30 * * * * php -f /var/www/nextcloud/occ face:background_job -t 900

You can verify if the cron job has been added and scheduled by executing:

# crontab -u www-data -l

Which returns:

[snip]
*/30 * * * * php -f /var/www/nextcloud/occ face:background_job -t 900

Note On some systems it might be required to call php-cli instead of php.

Note Please refer to the crontab man page for the exact command syntax.

systemd

If systemd is installed on the system, a systemd timer could be an alternative to a cronjob.

This approach requires two files: facerecognition.service and facerecognition.timer. Create these two files in /etc/systemd/system/.

facerecognition.service should look like this:

[Unit]
Description=Nextcloud FaceRecognition Job

[Service]
User=www-data
ExecStart=/usr/bin/php -f /var/www/nextcloud/occ face:background_job -t 900

Replace the user www-data with the user of your http server and /var/www/nextcloud/occ with the path to your current Nextcloud installation.

Note that the .service unit file does not need an [Install] section. Please check your setup because we recommended it in earlier versions of this admin manual.

facerecognition.timer should look like this:

[Unit]
Description=Run Nextcloud FaceRecognition every 30 minutes

[Timer]
OnBootSec=30min
OnUnitActiveSec=30min
Unit=facerecognition.service

[Install]
WantedBy=timers.target

The important parts in the timer-unit are OnBootSec and OnUnitActiveSec. OnBootSec will start the timer 5 minutes after boot, otherwise you would have to start it manually after every boot. OnUnitActiveSec will set a 5 minute timer after the service-unit was last activated.

Now all that is left is to start and enable the timer by running this command:

systemctl enable --now facerecognition.timer