Creating and configuring cron Jobs for task scheduling in Linux

In this article, we are going to learn how to configure Cron Jobs. We are going to use crontab to set up a Cron Job.

Configure Cron

  • Open your terminal and go to the /etc folder and check the /cron folders. You will see the following cron folders:
    • /etc/cron.hourly
    • /etc/cron.daily
    • /etc/cron.weekly
    • /etc/cron.monthly
  • Now, we will copy our shell script into one the preceding folders.
  • If you need to run your shell script to run daily, place it in the cron.daily folder. If you need to run it hourly, place it in the cron.hourly folder, and so on.
  • Example: Write a script and place it in the cron.daily folder. Make the script executable by giving the necessary permissions.
  • Now, run the crontab command:
$ crontab -e
  • Press Enter and it will ask for the editor of your type. By default, it will open vi editor. In my case, I selected nano. Now, create a cron command. The syntax for creating the cron command is:
    • The number of minutes after the hour (0 to 59)
    • The hour in military time (24 hour) format (0 to 23)
    • The day of the month (1 to 31)
    • The month (1 to 12)
    • The day of the week (0 or 7 is Sunday, or use the appropriate name)
    • The command to run
  • If you enter * in all options before the script name, you script will execute, every minute of every hour, of every day of the month, of every month and every day in the week.
  • Now, add the following line in your script:
* * * * * /etc/name_of_cron_folder/script.sh
  • Save the file and exit.
  • You can list the existing jobs by running the following command:
$ crontab -l
  • To remove the existing Cron Job, delete the line that contains your Cron Job and save it. Run the following command:
$ crontab -e

How it works

We used the crontab command to add and delete the Cron Job. Use the appropriate settings to execute your script daily, hourly, monthly, and weekly.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

14 − twelve =

Related Articles