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.
- Open your terminal and go to the
/etcfolder and check the
/cronfolders. You will see the following cron folders:
- 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.dailyfolder. If you need to run it hourly, place it in the
cron.hourlyfolder, and so on.
- Example: Write a script and place it in the
cron.dailyfolder. Make the script executable by giving the necessary permissions.
- Now, run the
$ crontab -e
- Press Enter and it will ask for the editor of your type. By default, it will open
vieditor. In my case, I selected
nano. Now, create a
croncommand. The syntax for creating the
- 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.