- Cron is a daemon that executes scheduled commands in linux.
- Also known as clock daemon.
- This daemon execute so many jobs known as cron jobs.
- This cron jobs are described in crontab files in UNIX systems.
How to add a cron job in crontab files?
- There are almost 7 categories. That are listed below.
- Minute : Add the minute here (eg : 12, 45)
- Hour : Add hour here (eg: 6, 17)
- Day Of month : Add day of month (eg : 10, 25)
- Month : Add month here (eg : 5, Dec, jan)
- Day Of Week : Add day of week (eg : 0=> sunday, 5 => friday)
- User : Add username
- Command : Add Your command here.
- An Example
Open the terminal and type
$ vim /etc/crontab
and add the following line (you can see the time by typing the date command)
50 15 * * * abhi sh /home/abhi/Project/my_project_no1/my_first_cron_job.sh
my my_first_cron_job.sh file contains:
cd /home/abhi/Projects/myProject/ && touch file1.txt file2.txt
Note : Don’t forget to give executable permission to my_first_cron_job.sh file
Here my my_first_cron_job.sh file executes at 15hr 50 min all days and creates two files named file1 and file2 in my /home/abhi/Projects/myProject folder.
# 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) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Note : You can also use * mark that denotes ‘all’. For example If you give 3 * * in day of month, month, day of week respectively ,that means it executes all month 3rd day whatever cron job you given.