What is cron?
I often need to add some scheduled job on server. I mean something that runs periodically, like every day or every hour.
You simply add a command that will run as scheduled and when I say command it can be anything that your system is
capable of running. Therefore, it can be shell command or script or even an java application. But let’s look closer how
could we set up our own cron task on linux. Luckily, unix systems have this package called cron
, that even comes
preinstalled on most modern linux systems.
Crontab
Crontab is a file that describes tasks scheduled by cron
and per crontab
command man page:
Each user can have their own crontab, and though these are files in
/var/spool/cron/crontabs
, they are not intended to be edited directly.
Therefore we will be using the command crontab -e
to open scheduled tasks file for editing. The -e
switch opens
the user’s crontab as other users crontab would be opened with crontab -u <otherusername>
command. Mine problem is
that linux opens in VI editor default (default editor set in linux environmnet) and I consider myself an younger
generation which never used VI editor magic wand and I also don’t want to spend few hours/days just to be able to make
changes in file. My favourite editor in unix systems is nano
. Therefore, we will use env variable or export variable
to override default editor VI. First, we will pass the EDITOR environment var to the command when I run it:
sudo env EDITOR=nano crontab -e
The second way is to set value of VISUAL
and then run the crontab like this:
export VISUAL=nano; crontab -e
Adding scheduled tasks
Now that we know how to edit cron tasks, let’s look at the syntax of using crontab.
* * * * * <command>
OR
* * * * * <path/to/script>
Those 5 stars mean the minute, the hour, the day, the month and the day of the week when the command
should be run.
Alternatively, you might specify the script with unix path. Each of those 5 values can represent a numeric value or
a star, where star represent wildcard.
1st | 2nd | 3rd | 4th | 5th | |
---|---|---|---|---|---|
ID | Minute | Hour | Day(of month) | Month | Day name (starts from Sunday) |
Everytime Value | * | * | * | * | * |
Specific Values | 0-59 | 0-23 | 1-31 | 1-12 | 0-6 |
Let’s dive deeper into those stars with some examples:
0 10 * * 1 apt update
The above example will update apt repository on my ubuntu system every Monday at 10am.
* * * * * /usr/bin/php /var/www/skey.uk/artisan cache:invalidate 1>> /dev/null 2>&1
The above example will call php script that invalidates cache of my website every minute every day.
0 10 1,15 * * certbot renew
The above example will try to renew SSL certificate of my website every 1st and 15th day of every month at 10:00am,
so twice a month.
0 9 * * 1,2,3,4,5 python -c 'import random; import time; time.sleep(random.random() * 300), Work.signPresence("Stefan")'
The above example will run my special log me in the job python code every workday at 9 a clock. After the script was
triggered by cron it will even wait random amount of seconds up to 5 minutes to make it more real. (I should probably
handle bank holidays as edge cases)
I hope this helps a bit and that’s my bit for today.