Mastering Cron Jobs for Beginners

Mastering Cron Jobs for Beginners

A cron job is a utility that allows users to schedule the running of scripts or programs in Linux or Unix-like Operating Systems. This feature is particularly beneficial for performing tasks on a regular schedule or automating repetitive activities, such as sending daily reports via email or backing up databases. The great advantage of using cron jobs is that they can operate in the background while also enabling us to create logs for these tasks.

Setting Up and Installing Cron on Ubuntu

Installing cron on Ubuntu.

sudo apt update && sudo apt install cron

To enable cron.

sudo systemctl enable cron

To check if cron daemon is running.

ps aux | grep cron

Components of cron job

A cron job mainly consists of these components.

1. Cron Daemon

Cron Daemon is a key background program that runs constantly to check the crontab, which lists scheduled tasks. It regularly reviews this file to see if any tasks need to be done at specific times. This way, Cron Daemon ensures that important activities, such as running scripts, sending emails, or performing maintenance, are done on time and efficiently. Its task management ability automates routine processes, allowing system administrators and users to concentrate on more important roles without worrying about doing these tasks manually.

2. Crontab

The crontab, or cron table, is a file that allows users to schedule repeating tasks at specific times. This feature helps automate regular operations, making work more efficient in various computer systems. It is managed using the crontab command.

3. Cron job entry

A cron job entry will consist of a schedule part with time and data, as well as the command to be run.

*  *  *  *  *  command_to_run
?  ?  ?  ?  ?
?  ?  ?  ?  ??? Day of the week    (0–7 or SUN–SAT) (Sunday = 0 or 7 or SUN)
?  ?  ?  ?????? Month              (1–12 or JAN–DEC)
?  ?  ????????? Day of the month   (1–31)
?  ???????????? Hour               (0–23)
??????????????? Minute             (0–59)

0 0 * * * /usr/bin/python3 /home/user/scripts/backup.py

# 1 0 * * * /usr/bin/python3 /home/user/scripts/backup.py

Below are some frequently used crontab commands for managing scheduled jobs:

# to list current crontab entires
crontab -l

# to edit crontab for the current user
crontab -e

# to remove current crontab
crontab -r

# to check the syntax
crontab -c

# to edit for a specific user
sudo crontab -u username -e

Examples

EntryDescription
0 0 * * *Runs the script every night at midnight.
0 10 * * 1Run the script every Monday at 10 AM.
30 16 * * FRIRun the script every Friday at 4 30 PM.
*/15 * * * *Run the script every 15 Minutes.
0 9-16 * 1-12 *At minute 0 past every hour from 9 through 16 in every month from January through December.
0 9-16 * * 1,3,5At minute 0 past every hour from 9 through 16 on Monday, Wednesday, and Friday.
0 0 * * 0Every Sunday at midnight

Other than the standard syntax we can also use the following:

Macro expressionDescriptionEquivalent Cron Expression
@hourlyRun once an hour at the beginning of the hour0 * * * *
@daily or
@midnight
Run once a day at midnight0 0 * * *
@weeklyRun once a week at midnight on Sunday morning0 0 * * 0
@monthlyRun once a month at midnight of the first day of the month0 0 1 * *
@yearly or
@annually
Run once a year at midnight of 1 January0 0 1 1 *

Test Your Understanding!

  1. 45 23 * * 0 /path/to/command
  2. */10 9-18 * * 1-5 /path/to/command
  3. 0 10-16 * 6-12,1-3 1
  4. Run a script every 15 minutes between 9 AM and 5 PM on weekdays.
  5. Run a task at 1:30 AM on the 1st and 15th of every month.
  6. Run every 5 minutes except between 2 AM and 3 AM.

Let us know your answer in the comments!

Conclusion

In this article, we explored the fundamentals of cron jobs, how they work, how to set them up, and how to use both standard syntax and macros to schedule tasks efficiently. We also looked at real-world examples and tips to help you automate your workflows effectively. With this knowledge, you’re now ready to create, manage, and troubleshoot cron jobs with confidence.

For quick help with cron syntax, visit Cron Guru—a useful tool to visualize and check your cron expressions.



Leave a Reply