Cron and Crontab in Linux

  1. Cron is a daemon that executes scheduled commands in linux.
  2. Also known as clock daemon.
  3. This daemon execute so many jobs known as cron jobs.
  4. 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.
      1. Minute : Add the minute here (eg : 12, 45)
      2. Hour : Add hour here (eg: 6, 17)
      3. Day Of month : Add day of month (eg : 10, 25)
      4. Month : Add month here (eg : 5, Dec, jan)
      5. Day Of Week : Add day of week (eg : 0=> sunday, 5 => friday)
      6. User : Add username
      7. 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.

Unknown's avatar

Author: Abhilash

Hi, I’m Abhilash! A seasoned web developer with 15 years of experience specializing in Ruby and Ruby on Rails. Since 2010, I’ve built scalable, robust web applications and worked with frameworks like Angular, Sinatra, Laravel, Node.js, Vue and React. Passionate about clean, maintainable code and continuous learning, I share insights, tutorials, and experiences here. Let’s explore the ever-evolving world of web development together!

Leave a comment