Do you wish to get up at 2 a.m., clear the logs, clear non permanent information, and run the identical server upkeep duties each single day?
Nicely, me neither. Nor do the hundreds of thousands of server admins who handle the 14+ billion servers internationally.
So, cease the insanity — I encourage you!
Cron jobs are constructed for that.
As a result of, genuinely, nothing says “competent sysadmin” like being quick asleep and taking credit score for the work your scripts deal with for you. It’s referred to as “using your sources.”
With cron jobs:
- Your boss thinks you’re devoted.
- Your server is aware of you’re lazy.
- You might have this lovely symbiotic relationship referred to as automation.
At the moment, you’re going to turn out to be a cron jobs professional.
First, What’s a Cron Job? (The Not-Boring Model)
A cron job is basically a job scheduler constructed into Unix-like working methods (Linux, macOS) that permits you to run Linux instructions robotically at specified instances and dates.
Consider it like a to-do record on your server, however…this one truly will get accomplished.
Cron in Metaphors
In case your server infrastructure have been a restaurant:
- The cron daemon is the supervisor checking the day by day schedule.
- The crontab is the workers task board.
- Every cron job is a job assigned to a selected workers member at a selected time.
- The command is the precise work being completed.
When the clock hits the scheduled time, the supervisor faucets the assigned worker on the shoulder and says, “It’s showtime!”
The worker then executes their job with out query or grievance.
If solely we people have been this dependable, the world can be a distinct place!
The Anatomy of a Cron Job
Each cron job consists of two foremost elements:
- When to run (the schedule)
- What to run (the command or script to execute)
The schedule makes use of a selected syntax that may seem like some laptop wizardry at first look:

However take a better look and it’ll begin to make sense.
Every asterisk may be changed with particular values, ranges, or intervals to create exactly the schedule you want.
Why Server Admins Love Cron Jobs
There’s a purpose why server admins (even me) get misty-eyed when discussing cron jobs.
They flip server administration into one thing that (a minimum of remotely) resembles work-life steadiness.
1. They Save You Time
Keep in mind time? That factor you by no means have sufficient of? Cron jobs give it again. You set them, you overlook them, and also you’re just about by no means taking a look at them.
(Nicely, till they break or it is advisable to change the schedule.)
2. They Keep Consistency
People are inconsistent. We overlook issues. We make typos. We get distracted by cat movies. Cron jobs carry out the precise job, the very same manner, each single time — no exceptions.
3. Your Server By no means Sleeps
With cron jobs, important upkeep occurs 24/7/365, whether or not you’re awake, asleep, or on a seaside sipping margaritas.
4. Error Logs > Human Reminiscence
Once you manually carry out duties, are you able to bear in mind precisely what you probably did and precisely once you did it? In all probability not.
However cron jobs may be configured to log their exercise, making a paper path of all automated actions for troubleshooting and verification.
5. They’re Constructed for Scalability
As your infrastructure grows, manually managing all the pieces turns into exponentially tougher. Cron jobs scale effortlessly.
Which means, the identical job can run throughout a number of servers with out requiring further time from you.
Setting Up Cron Jobs: A Step-by-Step Information
Sufficient idea! It is advisable to get your palms soiled with some sensible cron job setup.
Step 1: Affirm Cron Is Put in
Most Unix-like methods have cron pre-installed. To test if it’s accessible to be used, sort the under command:
crontab -e
Relying on the default editor, the command will open the crontab in your particular editor. In case you have by no means used crontab earlier than, it’d ask you to set the default editor.

If the terminal responds with command not discovered, you’ll want to put in cron with the under instructions:
- On Ubuntu/Debian:
sudo apt replace && sudo apt set up cron - On CentOS/RHEL:
sudo yum set up cronie
As soon as completed, begin and allow the cron service:
sudo systemctl begin cron
sudo systemctl allow cron
With the begin and allow instructions, we’re beginning the cron service to execute the cron jobs.
And with allow, we make it possible for even when your server restarts, the cron service robotically restarts with it, and no cron jobs are missed.
Nerd Observe: CentOS calls the cron service “crond”, so you have to to begin and allow the crond service.
Step 2: Understanding the Crontab
Alright, open the crontab or the crontable to start including your scheduled jobs.
Every person on the system can have their very own crontab file. Moreover, there’s a system-wide crontab.
To edit your private crontab:
crontab -e
This opens your crontab file in your default textual content editor. If that is your first time, select the nano editor (possibility 1) because it’s probably the most beginner-friendly.
For system-wide crontabs, run the under command with sudo privileges:
sudo nano /and so on/crontab

Step 3: Cron Job Syntax
We’ve already talked in regards to the primary construction within the anatomy of cron jobs earlier than.
However making a cron job may be complicated generally. Crontab.guru helps you visualize the job schedules as you sort them.

Now for the enjoyable half — writing our first cron job. Let’s check out some widespread cron job schedules:
Each minute:
* * * * /path/to/command
Each hour at minute 0:
0 * * * * /path/to/command
Day by day at midnight:
0 0 * * * /path/to/command
Each Monday at 3 a.m.:
0 3 * * 1 /path/to/command
Each quarter-hour:
*/15 * * * * /path/to/command
First day of each month at 6:30 a.m.:
30 6 1 * * /path/to/command
Step 4: Creating Your First Cron Job
Let’s transfer to making a easy backup cron job on your server.
The duty under creates a backup of your web site every single day at 2 a.m.
0 2 * * * tar -czf /path/to/backup/website-backup-$(date +%Ypercentmpercentd).tar.gz /path/to/your/web site
It’s going to output a compressed tar archive of your web site listing with the present date because the filename.
Step 5: Save and Confirm
Now, exit the editor. In nano, press Ctrl+X after which hit Y.
To view your present crontab and confirm your job was added:
crontab -l

That’s it! Your first cron job is now arrange and can run robotically on the scheduled time.
Sensible Cron Job Examples for Web site Managers
Now that the fundamentals, let’s discover some sensible cron jobs that may make your life as an internet site supervisor considerably simpler.
Database Backups
MySQL database backup (day by day at 1 a.m.):
0 1 * * * mysqldump -u username -p'password' database_name | gzip > /path/to/backups/db-backup-$(date +%Ypercentmpercentd).sql.gz
Log Rotation and Cleanup
Clear logs older than 7 days (weekly on Sundays):
0 0 * * 0 discover /path/to/logs -type f -name "*.log" -mtime +7 -delete
Web site Efficiency Monitoring
Examine web site response time each 5 minutes:
*/5 * * * * curl -o /dev/null -s -w "%{http_code} %{time_total}sn" instance.com >> /path/to/logs/website-performance.log
Content material Updates
Fetch and replace dynamic content material (each hour):
0 * * * * /path/to/content-update-script.sh
Electronic mail Studies
Ship a weekly visitors abstract each Monday at 9 a.m.:
0 9 * * 1 /path/to/generate-and-email-report.sh
Safety Scans
Run a safety scan script each night time at 3 a.m.:
0 3 * * * /path/to/security-scan.sh
Cron Job Finest Practices: Dos and Don’ts
To verify your cron jobs run easily and don’t trigger extra issues than they remedy, listed here are some necessary greatest practices.
The Dos
- At all times use full paths to instructions and information: Your cron surroundings doesn’t have the identical PATH as your person shell, so
“/usr/bin/python”is healthier than simply python. - Redirect output to forestall electronic mail spamming: By default, cron emails any output to the person. Add
>/dev/null 2>&1to suppress output or redirect to a log file as a substitute. - Check your instructions earlier than scheduling them: Run your command manually to make sure it really works as anticipated.
Add feedback to clarify every job — Future you’ll thank current you for documenting what every cron job does and why.
Every day database backup - Added by Jane on 2023-05-15
0 1 * * * /path/to/backup-script.sh
Think about using lockfiles for long-running jobs to forestall a brand new occasion from beginning if the earlier one continues to be working.
0 * * * * flock -n /tmp/script.lock /path/to/your/script.sh
The Don’ts
- Don’t schedule resource-intensive jobs throughout peak hours: Your backup doesn’t must run at midday when your website is busiest.
- Don’t use relative paths:
“./script.sh”will nearly definitely fail in cron. - Don’t overlook surroundings variables: Cron doesn’t load your .bashrc or .profile. Set any required variables within the crontab or script.
- Don’t overlook logging: With out correct logging, debugging cron jobs generally is a nightmare.
- Don’t overdo it: Too many frequent cron jobs can overload your server. Be strategic.
What To Do When Cron Jobs Go Incorrect
The one time it’s a must to look again at a cron job is when it breaks — and when it breaks, right here’s the right way to diagnose and repair widespread points.
Widespread Drawback #1: Job Doesn’t Run
Signs: Your scheduled job doesn’t appear to be executing in any respect.
Potential fixes:
- Examine cron daemon is working: The “systemctl” standing cron
- Confirm your crontab syntax: Use a software like crontab.guru
- Guarantee full paths to executables: Which command to seek out full paths
- Examine file permissions: Scripts have to be executable (chmod +x script.sh)
Widespread Drawback #2: Job Runs However Fails
Signs: The job executes however doesn’t full its job efficiently.
Potential fixes:
- Redirect output to a log file to see errors:
* * * * /path/to/script.sh > /path/to/script.log 2>&1 - Check the command manually with the identical surroundings
- Examine for dependencies that is perhaps lacking within the cron surroundings
Widespread Drawback #3: Electronic mail Flooding
Signs: Your inbox is flooded with cron output emails.
Potential fixes:
- Redirect output to null:
>/dev/null 2>&1 - Redirect to a log file:
>/path/to/logfile.log 2>&1
Solely electronic mail on errors:
* * * * /path/to/script.sh >/dev/null || echo "Script failed" | mail -s "Cron failure" you@instance.com
Widespread Drawback #4: Timing Points
Signs: Jobs run at sudden instances or frequencies.
Potential fixes:
- Double-check your timezone settings — date vs. cron’s expectation
- Pay attention to DST modifications that may have an effect on timing
- Use express time frames as a substitute of relative ones when precision issues
Superior Cron Job Writing Methods
We’ve regarded on the fundamentals, and you’re just about a professional with cron jobs by now. However this part will take you a step additional.
Utilizing Particular Strings
You don’t at all times want to put in writing cron jobs with these asterisk indicators. There are some particular strings that allow you to arrange cron jobs fairly simply.
- @yearly or @yearly: Run every year (0 0 1 1 *)
- @month-to-month: Run as soon as a month (0 0 1 * *)
- @weekly: Run as soon as per week (0 0 * * 0)
- @day by day or @midnight: Run as soon as a day (0 0 * * *)
- @hourly: Run as soon as an hour (0 * * * *)
- @reboot: Run as soon as at startup
For instance, in order for you one thing to run day by day, simply write the under command:
@day by day /path/to/daily-backup.sh
Atmosphere Variables in Crontab
To keep away from repeating a string over and over in your cron jobs (for instance, a selected path, or your admin electronic mail), arrange surroundings variables at the start of your crontab.
You may then reuse the variables as required inside your scripts or instructions.
SHELL=/bin/bash
PATH=/usr/native/sbin:/usr/native/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=admin@instance.com
# This job will ship errors to admin@instance.com
0 2 * * * /path/to/mailing_script.sh
If we use the surroundings variable MAILTO in our mailing_script.sh, the script will robotically ship an electronic mail to the right electronic mail handle.
With this, altering the admin electronic mail will solely require altering the worth of the MAILTO variable, as a substitute of constructing modifications throughout all scripts.
Working Jobs As Completely different Customers
In case you have superuser entry, you possibly can edit one other person’s crontab:
sudo crontab -u username -e
Utilizing Anacron for Machines That Aren’t At all times On
In contrast to cron, anacron ensures jobs run even when the pc was off through the scheduled time:
sudo apt set up anacron
Edit /and so on/anacrontab so as to add jobs that may run when the system comes again on-line.
Job Chaining for Complicated Workflows
Run jobs in sequence:
0 1 * * * /path/to/first-script.sh && /path/to/second-script.sh
Monitoring Cron Jobs
For critical server administration, take into account instruments like Cronitor that present monitoring and alerts on your cron jobs.
0 * * * * cronitor exec check-12345 -- /path/to/your/script.sh
Let’s Speak Prices
Cron jobs can’t exist in isolation. They want a server and a service working on a server that it is advisable to handle.
Now, if you happen to’re studying this text, it’s extremely possible that you’ve got a server on your web site or utility.
Actually, if you happen to’re internet hosting with DreamHost VPS or any Linux-based internet hosting supplier, you’ve already bought all the pieces it is advisable to get began with automating your server administration duties.
If not, a $10/month VPS is all you’d want, particularly when beginning out.
For these already working a DreamHost VPS, the method couldn’t be extra easy:
- SSH into your server
- Run crontab -e to edit your private cron desk
- Add your scheduled duties
- Save, and let the automation start!
SSH
Safe Shell Protocol (SSH) is a cryptographic community protocol for working companies securely by means of an unsecured community. It’s largely used for command-line executions and distant logins.
That’s it. The infrastructure you’re already paying for immediately turns into extra useful, extra environment friendly.
Your Server’s New Autopilot
Congratulations!
You’ve graduated from guide labor to automation wizardry. With cron jobs dealing with the routine upkeep, backups, and monitoring, you possibly can deal with rising your web site and enterprise reasonably than babysitting the server.
And bear in mind, it’s going to be a course of. The automation will turn out to be extra subtle as you add increasingly more duties to it.
However for now, begin with a number of important cron jobs, monitor how they carry out, and progressively broaden your automation as you develop extra snug with the method.
Now go on and take that nap, since you simply saved your self a buttload of time.
Did you get pleasure from this text?
