Looking Into Cron Jobs
In a previous post I explored the concept of executing a bash command with double-click. For certain scenarios, the requirement is for the bash script to be executed automatically at specified intervals. For this, what is commonly used is cron.
Yesterday I went through a video to learn cron. I will write in this mini-post notes for basic use of it.
Basic Commands
To see the list of cron jobs.
crontab -l
To edit the “tab” or list of cron jobs.
crontab -e
Writing Jobs in crontab
files
The best visual way I found of explaining how to write a line in the crontab files (each line being a cron job) is the following snippet from the page in Wikipedia.
# ┌───────────── minute (0–59)
# │ ┌───────────── hour (0–23)
# │ │ ┌───────────── day of the month (1–31)
# │ │ │ ┌───────────── smonth (1–12)
# │ │ │ │ ┌───────────── day of the week (0–6) (Sunday to Saturday;
# │ │ │ │ │ 7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>
An example that is not very restrictive
For a command to run every day at 9:05
5 9 * * * echo "hello world"
Adding more restrictions
For a command to run every year , on the 15th of August at 9:05 IF THAT DAY IS A FRIDAY! (most year wont run)
5 9 15 8 5 echo "hello world"
Simpler way of writing a cron job
There are nonstandard scheduling definitions (@daily
, @reboot
, etc.). Example below:
@reboot echo "hello world"