V5 Cron Setup Guide - Debian 10.x Self-host - CLI Access req'd

Just a little guide to help configure and log the cron job for v5. If you’ve setup your cron job and you’re seeing cron failures each minute in /var/log/syslog, this may help you debug.

Cron Daemon Logs

Firstly enable cron event logging in syslog. This logs all messages from the cron daemon (should be every minute if configured as per the docs here).

$ sudo nano /etc/rsyslog.conf

# Uncomment the line below
cron.*                          /var/log/cron.log

<ctrl+o to save>
<ctrl+x to exit>

Restart the service:

sudo systemctl restart rsyslog

Now we can view cron log entries (tail -f will update output in real time):

$ sudo tail -f /var/log/cron.log

Cron Output Logs

Now we want to see the cron job output. This is the information that will help you confirm that the cron task is completing successfully and debug if not. You may already have your mailbox filling up with error reports if you have a mail server configured. Let’s redirect these to a logfile instead:

Note: Not using sudo

$ crontab -e

Enter the cron job on a new line

# This cron supports invoice ninja functionality
* * * * * cd /var/www/invoice.example.com/invoiceninja && php artisan schedule:run >> /var/log/cronoutput.log 2>&1

This logfile will grow quickly, so lets also create a job to empty the file once daily:

# Clear cronoutput.log at 0500 each day
* 5 * * * echo "" > /var/log/cronoutput.log >> /dev/null 2>&1

The logging will fail because your user doesn’t have permissions to write to /var/log. The solution is to manually create the logfile, take ownership and set permissions:

Note: Now using sudo
Note: Replace ‘user’ with your user name

$ sudo touch /var/log/cronoutput.log
$ sudo chown 'user' /var/log/cronoutput.log
$ sudo chmod 744 /var/log/cronoutput.log

Now we can view cron log outputs:

$ tail -f /var/log/cronoutput.log

Possible Error

My cron output was showing an error for lack of write permissions to laravel.log. This logfile is located in ~/invoiceninja/storage/logs/ (it’s also very helpful for debugging Invoice Ninja).

By now the Invoice Ninja/domain directory structure should be owned by www-data:www-data so nginx can serve as intended. If you haven’t already, then you might want to set correct file and directory permissions now:

Note: Replace /invoice.example.com with your web root

$ sudo chown -R www-data:www-data /var/www/invoice.example.com
$ sudo find /var/www/invoice.example.com -type d -exec chmod 755 {} \;
$ sudo find /var/www/invoice.example.com -type f -exec chmod 644 {} \;

To give our user permissions so that the cron (which runs as our user) is able to write to the logfile, we want to add our user to the www-data group in the same way you would configure an FTP user for example. This way our user can modify files in the web directories without needing to change ownership of files. You may not want to do this for standard non-admin users but then, you wouldn’t be here anyway.

Note: Replace ‘user’ with your user name

$ sudo usermod -a -G www-data 'user'

Now we ensure the group has write permissions to the logfile:

$ sudo chmod g+w /var/www/invoice.example.com/invoiceninja/storage/logs/laravel.log

Now test (without sudo):

$ nano /var/www/invoice.example.com/invoiceninja/storage/logs/laravel.log

If the nano editor opens with [ Read xxx lines ] and no write access warning, everything went as expected.

Success

Now you are able to debug cron tasks and address any permission errors. Your /var/www/cronoutput.log file will be populating with entries like this:

$ cat cronoutput.log

No scheduled commands are ready to run.
No scheduled commands are ready to run.
Running scheduled command: '/usr/bin/php7.4' 'artisan' queue:restart > '/dev/null' 2>&1
Running scheduled command: Turbo124\Beacon\Jobs\BatchMetrics
Running scheduled command: '/usr/bin/php7.4' 'artisan' queue:work > '/dev/null' 2>&1
No scheduled commands are ready to run.
No scheduled commands are ready to run.
No scheduled commands are ready to run.
Running scheduled command: '/usr/bin/php7.4' 'artisan' queue:restart > '/dev/null' 2>&1
Running scheduled command: Turbo124\Beacon\Jobs\BatchMetrics

I hope this helps somebody :cowboy_hat_face:

1 Like

This is awesome, thanks!

We’re starting to work on the v5 docs and will definitely link to this.

1 Like

Cool, I’m just happy to contribute something. Also, when I forget what I did I can refer back :laughing:

1 Like