If you’ve ever needed to automate tasks on your server or computer, you may have come across the term “cron job”. Cron jobs are a powerful tool for scheduling and automating tasks, and in this beginner’s guide, we’ll cover everything you need to know to get started
Table of Contents
What is a Cron Job?
A cron job is a scheduled task that runs automatically on a recurring basis. It’s a utility commonly used in Unix-like operating systems (such as Linux) that allows users to schedule scripts, programs, or commands to run at specific intervals, ranging from once a minute to once a year.
How to Create a Cron Job

To create a new cron job, you’ll need to use the crontab command, which is used to manage your system’s crontab file. The crontab file is a simple text file that lists the commands or scripts you want to run, along with the schedule for when they should be run.
To open your crontab file for editing, simply type the following command in your terminal:
crontab -e
This will open the crontab file in your default text editor. To add a new cron job, simply add a new line to the file with the following format:
* * * * * /path/to/command arg1 arg2
Crontab Syntax

Before setting up cron jobs, you must understand cron’s syntax and formatting to ensure the script runs properly. The crontab syntax consists of five fields with the following possible values:
- Minute. The minute of the hour the command will run, ranging from 0-59.
- Hour. The hour the command will run, ranging from 0-23 in a 24-hour notation.
- Day of the month. The date of the month the user wants the command to run, ranging from 1-31.
- Month. The month that the user wants the command to run. It ranges from 1-12, representing January until December.
- Day of the week. The day of the week for a command to run, ranging from 0-6. The value represents Sunday-Saturday. In some systems, the value 7 represents Sunday.
Cron Syntax Examples
To help you better understand the cron syntax, here’s a list of sample commands to conduct system management with cron jobs:
Example | Explanation |
0 0 * * 0 /root/backup.sh | Perform a backup every Sunday at midnight. |
0 * * * 1 /root/clearcache.sh | Clear the cache every hour on Mondays. |
0 6,18 * * * /root/backup.sh | Backup data twice a day at 6 am and 6 pm. |
*/10 * * * * /scripts/monitor.sh | Perform monitoring every 10 minutes |
*/15 * * * * /root/backup.sh | Perform a backup every 15 minutes. |
* * 20 7 * /root/backup.sh | Perform a backup every minute on July 20. |
0 0 * * 2 * /root/backup.sh | Perform a backup at midnight every Tuesday. |
* * * 1,2,5 * /scripts/monitor.sh | Perform monitoring every minute in January, February, and May. |
10-59/10 5 * * * /root/clearcache.sh | Clear the cache every 10 minutes at 5 am, starting from 5:10 am. |
0 8 1 */3 * /home/user/script.sh | Make the task run quarterly on the first day of the month at 8 am. |
0 * * * * /root/backup.sh | Create a backup every hour. |
* * * * * /scripts/script.sh; /scripts/scrit2.sh | Include multiple tasks in a single cron job. This is useful for scheduling multiple tasks to run at the same time. |
@reboot /root/clearcache.sh | Clear the server cache every time you turn on the system. |
0 8 1-7 * 1 /scripts/script.sh | Run a script on the first Monday of each month at 8 am. |
5 4 * * 0 /root/backup.sh | Create a backup every Sunday morning at 4:05 am. |
15 9 1,20 * * /scripts/monitor.sh | Perform monitoring at 9:15 pm on the 1st and 20th of every month. |
@hourly /scripts/monitor.sh | Perform monitoring every hour. |
0 0 1,15 * 3 /scripts/script.sh | Run a script at midnight every Wednesday between the 1st and 15th of every month. |
15 14 1 * * /root/clearcache.sh | Clear the cache on the first day of every month at 2:15 pm. |
15 6 1 1 * /root/backup.sh | Perform a backup every January 1st at 6:15 am. |
0 0 * * * /scripts/monitor.sh | Run the monitoring script once a day at midnight. |
0 0 15 * * /root/clearcache.sh | Clear the cache at midnight on the 15th of every month. |
Tips and Best Practices
- Always test your cron jobs before putting them into production, to ensure they’re running correctly.
- Use full paths to your scripts and commands in your cron jobs, to avoid any issues with the system’s environment variables.
- Avoid scheduling multiple jobs at the same time, as this can cause performance issues on your server.
- Use logging or email notifications to monitor the output of your cron jobs, to ensure they’re running correctly and to diagnose any issues.
Conclusion
Cron jobs are a powerful tool for automating tasks on your server or computer, and can save you a lot of time and effort in the long run. With the tips and best practices outlined in this guide, you should be able to create and manage your own cron jobs with confidence in webhostingcochin