intro to cron
Cron is the name of program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date. -http://www.unixgeeks.org/security/newbie/unix/cron-1.html
I found a use case for cron
today but its always good to learn the fundamentals before running.
So let’s experiment in a basic Linux sandbox environment with cron
installed:
ls etc
and you will see a bunch of cron
listings. the .daily
.monthly
etc are directories where scripts can be placed that will run as expected based on the directory name.
For finer grain control a user can supply their own crontab
. According to the man page, cron jobs are driven by tables hence the crontab
name. These custom files can be placed in the cron.d
directory.
Let’s create a custom cron job. For this experiment let’s create a log file where we can log the results of our scheduled task:
Now let’s have a look at the existing crontab
file:
We see this is a system-wide crontab, and also that any changes we make will be loaded up automatically. that’s cool.
At the bottom of this file, we see some table listings such as:
The 17 * * * *
is the schedule declaration specifies when the job will run. In this case, at minute 17 every hour (hence this is the command that runs jobs in the cron.hourly
directory.)
The cron
scheduling system is fairly cryptic. I found this resource to be very helpful: https://crontab.guru.
Let’s add our own “job” that runs every minute. At the bottom of the file use vim’s insert mode to add the following line:
Exit vim. Back at the terminal, let’s start the cron
utility
Let’s wait 60 seconds then inspect the contents of our log file:
There’s our output. Keep checking back. As long as the container is running, new lines will be logged every minute.