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.

Advertisement

Author: Abhilash

I'm Abhilash, a web developer who specializes in Ruby development. With years of experience working with various frameworks like Rails, Angular, Sinatra, Laravel, NodeJS, React and more, I am passionate about building robust and scalable web applications. Since 2010, I have been honing my skills and expertise in the Ruby on Rails platform. This blog is dedicated to sharing my knowledge and experience on topics related to Ruby, Ruby on Rails, and other subjects that I have worked with throughout my career. Join me on this journey to explore the exciting world of web development!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: