Remember those days, before the advent of Android or I-phone, when we used to schedule birthday wishes to our friends at midnight?
Maybe we have not come that far, even these days we schedule our emails. Our daily alarms, calendar apps, meeting apps - All these are a kind of cron jobs that we are utilizing without noticing!
What Are Cron Jobs?
A Cron (Cron Job) is a command line utility in Linux used for scheduling tasks to be executed in the future. They are mostly used for repetitive or periodical tasks. Cron jobs come in handy when we want to send an email to our office every day automatically, run a script with a welcome message when we boot our computer or take daily backups without backing it up manually. Cron jobs could do all the work for us.
Navigating Cron
To check if the cron is active - systemctl status cron
To edit cron jobs - crontab -e
To select the preferred editor - select editor
Adding Cron Job
Let's create our cron job.
minutes - 26
hours - 17
day of the month - *
the month of the year - *
day of the week - 3 (Monday starts from 1)
command - touch command is used to change file timestamps, in simple words, it creates a file. Here the file (lol.txt) will be created in the CronTest directory.
'*' - It is a wildcard, signifying all days of the month and all months of the year.
As intended the file has been created at 17:26 on a Wednesday.
cron.deny & cron.allow
Root users could create cron.deny file - sudo vim /etc/cron.deny
and prevent other users on the system from creating cron jobs. They could also create cron.allow file - sudo vim /etc/cron.allow
by letting users create cron jobs; cron.allow has priority over cron.deny.