Basics
What is a Cron Job?
Lets start with the definition you’ll find everywhere over the internet
Cron is a work schedule hypervisor that runs assignments at prescribed times. These activities are referred to as Cron jobs, and they are typically used to optimize security management or management.
The scheduling of these tasks is managed by the cron daemon, a time-based job scheduler.
Now these definitions and these jargons are easy to get if you’re already familiar with Cron jobs, but i believe unless you can’t explain it to a 5 years old.. you’re not familiar with concept at all.. so let me ask you.. will you be able to explain the concept to a 5 years old?
![](https://sakshampublicdocuments.s3.ap-south-1.amazonaws.com/kid-what-gif.gif)
Here is how I would do it !
Imagine you have a magic calendar that can make things happen all by itself. You can tell this magic calendar to do certain tasks for you at specific times.
For example:
- You can tell it to wake you up every day at 7:00 AM.
- Or you can ask it to water your plants every evening at 6:00 PM.
These special instructions are called “cron jobs” and the special calender is called “cron daemon” which manages all of this.
How Does Daemon function?
![](https://sakshampublicdocuments.s3.ap-south-1.amazonaws.com/daemon-targaryen-house-of-the-dragon.gif)
Nope.. not the daemon above.. I was talking about the cron daemon. The above daemon is surely going to kill the high towers and then sit on the throne himself.. anyways.. coming back to Cron Daemon
The cron daemon is a time-based job scheduler that runs as a background process. It continuously runs and checks for scheduled tasks that need to be executed. The daemon wakes up every minute and checks the schedule to see if any commands need to be run at that time.
- Cron Tab Files
- User Crontabs: Each user, including the root user, can have their own crontab file. These are typically managed using the crontab command (e.g., crontab -e to edit, crontab -l to list).
- System Crontab: There’s a system-wide crontab file located at /etc/crontab, which can be edited directly. Additionally, scripts in the /etc/cron.d/ directory are treated like individual crontab files.
- Crontab Syntax : The crontab file uses a specific syntax to define the timing and command of cron jobs. Each line in a crontab file consists of:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7) (Sunday = 0 or 7)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
Wildcards (*) mean “every” (e.g., every minute, every hour)
PS: Cron Jobs at min can be executed at a difference of 1 minute.. not less than that.
- Cron Daemon Operations :
- Startup: When the cron daemon starts, it reads the crontab files and keeps them in memory.
- Wake Up: The daemon wakes up every minute to check if any jobs need to be run.
- Execution: When a job’s scheduled time arrives, the cron daemon executes the command specified in the crontab entry. It uses the environment and user permissions of the user who owns the crontab file.
Now answer me this.. can you explain the above to a 5 years old (basically what i mean is..do you yourself understand it well enough or did you just mugged it up.. or worse.. just parsed through it)?
- Crontab Files:
- Imagine you have a special notebook where you write down your daily chores.
- Each person in your family can have their own notebook to write their chores.
- There’s also a big family notebook where important chores for everyone are written.
- Crontab Syntax:
- When you write chores in the notebook, you need to say exactly when to do them.
- For example, you can write “Feed the cat at 7 AM every day” or “Water the plants every Sunday at 5 PM.”
- Cron Daemon Operation:
- Recall your magic calender now who reads everyone’s notebooks and remembers all the chores.
- This magic calender checks the time every minute to see if it’s time to do any chores.
- When the time comes, the magic calender makes sure the chore gets done exactly as written.
How does crond wakes up every minute?
Now i’m not going to explain the part below to a 5 years old.. i’ll prefer teaching the kid basics of linux itself 🫡.. there is not much magic here .. and kid can’t be this stupid too 🤓
Initialization: When the cron daemon starts, it initializes itself and reads the configuration files (/etc/crontab, /etc/cron.d/*, and user crontabs).
Internal Sleep Mechanism: After initialization, the cron daemon enters an infinite loop where it primarily sleeps until the next minute. This is typically implemented using a sleep function within the loop.
System Timer: The sleep function is based on system timers provided by the operating system. These timers are highly accurate and wake up the process after the specified duration. The cron daemon uses a sleep duration of approximately 60 seconds, but it aligns itself with the start of the next minute.
Checking the Time: Upon waking up, the cron daemon checks the current system time. It compares the current time to the schedule of each cron job to determine if any jobs are due to be executed.
Executing Scheduled Jobs: If the current time matches any of the job schedules, the cron daemon executes those jobs. It forks a child process for each job to ensure that the main daemon continues running and is ready to check the schedule again at the next minute mark.
Resuming Sleep: After executing any due jobs, the cron daemon goes back to sleep until the next minute. This cycle continues indefinitely.
Well thats all in this one.. let me know where I messed up in writing this one.. or reach out if you have any comments otherwise too :)