Do you need to get up at 2 a.m., clear the logs, clear momentary information, and run the identical server upkeep duties each single day?
Effectively, 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’ve this stunning symbiotic relationship referred to as automation.
Right now, 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 actually a job scheduler constructed into Unix-like working programs (Linux, macOS) that allows you to run Linux instructions routinely at specified occasions and dates.
Consider it like a to-do record to your server, however…this one really will get accomplished.
Cron in Metaphors
In case your server infrastructure had been a restaurant:
- The cron daemon is the supervisor checking the every day schedule.
- The crontab is the employees project board.
- Every cron job is a job assigned to a particular employees member at a particular time.
- The command is the precise work being performed.
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 criticism.
If solely we people had been this dependable, the world can be a special place!
The Anatomy of a Cron Job
Each cron job consists of two essential components:
- When to run (the schedule)
- What to run (the command or script to execute)
The schedule makes use of a particular syntax that may seem like some pc wizardry at first look:
However take a more in-depth look and it’ll begin to make sense.
Every asterisk could be changed with particular values, ranges, or intervals to create exactly the schedule you want.
Why Server Admins Love Cron Jobs
There’s a motive why server admins (even me) get misty-eyed when discussing cron jobs.
They flip server administration into one thing that (at the very least remotely) resembles work-life stability.
1. They Save You Time
Bear in mind time? That factor you by no means have sufficient of? Cron jobs give it again. You set them, you neglect them, and also you’re just about by no means taking a look at them.
(Effectively, till they break or it’s essential change the schedule.)
2. They Preserve Consistency
People are inconsistent. We neglect 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 seashore 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 whenever you did it? Most likely not.
However cron jobs could 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 things turns into exponentially harder. Cron jobs scale effortlessly.
Which means, the identical job can run throughout a number of servers with out requiring extra time from you.
Setting Up Cron Jobs: A Step-by-Step Information
Sufficient concept! You could get your palms soiled with some sensible cron job setup.
Step 1: Verify Cron Is Put in
Most Unix-like programs have cron pre-installed. To examine if it’s obtainable to be used, sort the beneath command:
crontab -e
Relying on the default editor, the command will open the crontab in your particular editor. If 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 beneath instructions:
- On Ubuntu/Debian:
sudo apt replace && sudo apt set up cron
- On CentOS/RHEL:
sudo yum set up cronie
As soon as performed, 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 guarantee that even when your server restarts, the cron service routinely restarts with it, and no cron jobs are missed.
Nerd Observe: CentOS calls the cron service “crond”, so you’ll need 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 beneath command with sudo privileges:
sudo nano /and so forth/crontab

Step 3: Cron Job Syntax
We’ve already talked concerning the primary construction within the anatomy of cron jobs earlier than.
However making a cron job could be complicated typically. 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 to your server.
The duty beneath creates a backup of your web site day-after-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 routinely on the scheduled time.
Sensible Cron Job Examples for Web site Managers
Now that you realize the fundamentals, let’s discover some sensible cron jobs that may make your life as a web site supervisor considerably simpler.
Database Backups
MySQL database backup (every 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
Test 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
E-mail Experiences
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 evening at 3 a.m.:
0 3 * * * /path/to/security-scan.sh
Cron Job Greatest Practices: Dos and Don’ts
To verify your cron jobs run easily and don’t trigger extra issues than they clear up, listed below are some essential finest practices.
The Dos
- At all times use full paths to instructions and information: Your cron setting doesn’t have the identical PATH as your person shell, so
“/usr/bin/python”
is best than simply python. - Redirect output to stop e mail spamming: By default, cron emails any output to the person. Add
>/dev/null 2>&1
to suppress output or redirect to a log file as an alternative. - Take a look at your instructions earlier than scheduling them: Run your command manually to make sure it really works as anticipated.
Add feedback to elucidate 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 stop a brand new occasion from beginning if the earlier one continues to be operating.
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 virtually definitely fail in cron. - Don’t neglect setting 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 is usually 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 important to look again at a cron job is when it breaks — and when it breaks, right here’s learn how to diagnose and repair widespread points.
Frequent Drawback #1: Job Doesn’t Run
Signs: Your scheduled job doesn’t appear to be executing in any respect.
Potential fixes:
- Test cron daemon is operating: 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
- Test file permissions: Scripts have to be executable (chmod +x script.sh)
Frequent 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
- Take a look at the command manually with the identical setting
- Test for dependencies that is perhaps lacking within the cron setting
Frequent Drawback #3: E-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 e mail on errors:
* * * * /path/to/script.sh >/dev/null || echo "Script failed" | mail -s "Cron failure" you@instance.com
Frequent Drawback #4: Timing Points
Signs: Jobs run at sudden occasions or frequencies.
Potential fixes:
- Double-check your timezone settings — date vs. cron’s expectation
- Pay attention to DST adjustments that may have an effect on timing
- Use express time frames as an alternative of relative ones when precision issues
Superior Cron Job Writing Strategies
We’ve seemed on the fundamentals, and you might be 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 write down cron jobs with these asterisk indicators. There are some particular strings that allow you to arrange cron jobs fairly simply.
- @yearly or @yearly: Run yearly (0 0 1 1 *)
- @month-to-month: Run as soon as a month (0 0 1 * *)
- @weekly: Run as soon as every week (0 0 * * 0)
- @every 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, if you need one thing to run every day, simply write the beneath command:
@every day /path/to/daily-backup.sh
Surroundings Variables in Crontab
To keep away from repeating a string time and again in your cron jobs (for instance, a particular path, or your admin e mail), arrange setting variables in the beginning of your crontab.
You possibly can 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 setting variable MAILTO in our mailing_script.sh, the script will routinely ship an e mail to the proper e mail tackle.
With this, altering the admin e mail will solely require altering the worth of the MAILTO variable, as an alternative of constructing adjustments throughout all scripts.
Working Jobs As Totally different Customers
If in case you have superuser entry, you may edit one other person’s crontab:
sudo crontab -u username -e
Utilizing Anacron for Machines That Aren’t At all times On
Not like cron, anacron ensures jobs run even when the pc was off in the course of the scheduled time:
sudo apt set up anacron
Edit /and so forth/anacrontab so as to add jobs that can run when the system comes again on-line.
Job Chaining for Advanced Workflows
Run jobs in sequence:
0 1 * * * /path/to/first-script.sh && /path/to/second-script.sh
Monitoring Cron Jobs
For severe server administration, think about instruments like Cronitor that present monitoring and alerts to your cron jobs.
0 * * * * cronitor exec check-12345 -- /path/to/your/script.sh
Let’s Discuss Prices
Cron jobs can’t exist in isolation. They want a server and a service operating on a server that it’s essential handle.
Now, for those who’re studying this text, it’s extremely probably that you’ve a server to your web site or software.
Actually, for those who’re internet hosting with DreamHost VPS or any Linux-based internet hosting supplier, you’ve already bought all the things it’s essential 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 operating a DreamHost VPS, the method couldn’t be extra simple:
- 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 operating companies securely by way of an unsecured community. It’s principally used for command-line executions and distant logins.
That’s it. The infrastructure you’re already paying for abruptly turns into extra worthwhile, 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 may give attention to rising your web site and enterprise relatively than babysitting the server.
And bear in mind, it’s going to be a course of. The automation will turn out to be extra refined as you add increasingly duties to it.
However for now, begin with a couple of important cron jobs, monitor how they carry out, and steadily increase 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 take pleasure in this text?