Appearance
Cron
IxDF mainly uses Laravel’s scheduler to run scheduled jobs. However, even the scheduler needs to be ran by a cron. So, in a general sense, IxDF uses Crontab to schedule cron jobs, even the general one, added for Laravel Scheduler. You will find the required cronjobs here, as well as to add a new cron job through Crontab.
Default crontab
This is the cron rules which should reside in the server’s crontab to make our platform work smoothly:
text
* * * * * echo "[$(date)]" >> /home/forge/logs/cron.log && /usr/bin/php /home/forge/www.interaction-design.org/artisan schedule:run 1>> /home/forge/logs/cron.log 2>&1 MAILTO="development@interaction-design.org"
30 0 * * * bash /home/forge/www.interaction-design.org/current/scripts/database/backup.sh
5 4 * * 7 echo "SUDO_PASSWORD" | sudo -s bash /home/forge/www.interaction-design.org/current/scripts/ssl/generate-ssl-for-aws-cloudfront.sh- The first rule is to run Laravel Scheduler, so that the scheduled jobs in Laravel can be ran.
- The second and third rules are to get daily database backups.
- The forth one is to auto-renew SSL certificates weekly for our media distributions on AWS.
How to add a new cron job
Use Forge UI
The simplest option. Forge makes it easy to add and remove jobs and creates dedicated logs for every cron job. Example URLs
- production server: https://forge.laravel.com/servers/796468#/scheduler
- staging server: https://forge.laravel.com/servers/795724#/scheduler
Use terminal
Have a job which will be needed to run in a fixed interval? Cool! You’re in the right place. There are two different type of cron jobs:
- Jobs whose code resides in the codebase. These should go into Laravel’s Scheduler. Here you can find how to schedule a job inside Laravel’s Scheduler.
- Jobs whose code does not reside in the codebase. Well... They could actually reside in the codebase, but not in Laravel’s codebase. These jobs include bash scripts, for example. You have a bash script with a full path of
/home/forge/www.interaction-design.org/current/scripts/code/run-weekly.sh? That’s kind of job we’re going to explain how to be scheduled in this tutorial.
First, make SSH connection into the server you want to schedule the job. Find out how to do it here.
After you’re in, run this command to edit system crontab file:
bash
# system-wide crontab
sudo vi /etc/crontab
# current user’s crontab
crontab -eThis will open up a text file, preferably by a text editor like vi. You should be seeing something like this:
text
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron’s system
# daemon’s notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow commandTo add a new cron job, move to the bottom of the file, and create a newline. You will need a cron rule to add here. A cron rule will contain the time interval, and an executable line of command. To create a crontab-applicable time interval, you can use Crontab Generator. For this tutorial, let’s run the command on sundays every week, at 04:05 AM UTC.
To do that, add this line into the file:
text
5 4 * * 7 bash /home/forge/www.interaction-design.org/current/scripts/code/run-weekly.shSave the file, and quit. To do that in a text editor like vi or vim: Hit Esc, then type :wq and then hit Enter. The cron job is now added!