Crontab or Cronjob

Cron is a system daemon used to execute desired tasks (in the background) at designated times.


A crontab file is a simple text file containing a list of commands meant to be run at specified times. It is edited using the crontab command. The commands in the crontab file (and their run times) are checked by the cron daemon, which executes them in the system background.


 How to add a new cronjob ?


[simterm]$crontab -e[/simterm]


Above command will bring you to default editing programs to add a new cronjob.


A simple "job" in cronjob is executed with scripting of Bash language.



For example I have a disk check script as below :



#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
mail -s 'Disk Space Alert' [email protected] << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi


and saved the script under /script/checkdisk.sh

Then, we launch [simterm]$crontab -e[/simterm] insert [simterm]0 5 * * * /script/backup.sh[/simterm] save it.

Above command will launch the script every 5 a.m everyday.

How is the formatting ?

Refer to illustration below
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |
# * * * * * user-name command to be executed


and
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}


List created cronjob?

[simterm]$crontab -l[/simterm]

Thank you.