>
Cron is a Linux utility that runs commands or scripts automatically at specified intervals. Whether you need to back up databases every night, delete temporary files weekly, or check system resources hourly, cron makes it possible without manual intervention.
A cron job is a scheduled task that executes in the background according to a predefined schedule. The cron daemon (crond) runs continuously on your Linux system and checks every minute whether any scheduled tasks need to execute. Unlike one-time scheduling, cron handles recurring tasks—daily, weekly, monthly, or at custom intervals.
You've likely encountered cron jobs if you've worked with servers. System updates, log rotation, backup scripts, and cleanup routines almost always rely on cron. It's one of the most reliable ways to automate Linux administration tasks.
Cron jobs are defined in a special file called a crontab (cron table). Each user on a Linux system can have their own crontab file, and there's also a system-wide crontab. These files contain lines that specify what command to run and when.
User crontabs are stored in /var/spool/cron/crontabs/ and are edited using the crontab command. System-wide crontabs live in /etc/cron.d/ and can be edited directly as root. The difference is that system crontabs include a username field, allowing the same file to run commands as different users.
Each line in a crontab file represents one scheduled task. The syntax looks intimidating at first, but it's actually straightforward once you understand the five time fields:
minute hour day-of-month month day-of-week command
Here's what each field means:
Let's look at practical examples. To run a backup script every day at 2:30 AM:
30 2 * * * /home/user/backup.sh
Breaking this down: minute=30, hour=2, day-of-month=* (any), month=* (any), day-of-week=* (any). The asterisk (*) means "all values" for that field.
To run a job every Monday at 9:00 AM:
0 9 * * 1 /usr/local/bin/weekly-report.sh
To run something every 15 minutes:
*/15 * * * * /usr/local/bin/check-health.sh
The */15 means "every 15 minutes." You can use this pattern with any field.
To run at 6:00 PM and 9:00 PM daily:
0 18,21 * * * /home/user/send-notification.sh
Commas separate multiple values in the same field.
Creating a cron job is simple. Use the crontab -e command to open your user's crontab in your default text editor:
crontab -e
Your editor will open (usually nano or vi). Add a new line with your cron job syntax, save, and exit. That's it—the job is scheduled immediately.
To see what jobs you've already scheduled:
crontab -l
This lists all cron jobs for the current user.
To delete all cron jobs for the current user:
crontab -r
To remove just one job, use crontab -e and manually delete that line.
If you need to schedule tasks that require elevated privileges, use sudo crontab -e:
sudo crontab -e
This opens the root user's crontab. Root cron jobs have access to system-level operations like shutting down services or modifying system files.
Back up a MySQL database every day at 3 AM:
0 3 * * * mysqldump -u user -p password mydb > /backups/mydb-$(date +\%Y\%m\%d).sql
This creates timestamped backup files so you don't overwrite old backups.
Clear application logs every Sunday at midnight:
0 0 * * 0 > /var/log/app.log
This truncates the log file. For more sophisticated log management, use logrotate instead.
Run a monitoring script every hour:
0 * * * * /usr/local/bin/health-check.sh
Renew SSL certificates on the first day of each month at 2 AM:
0 2 1 * * /usr/local/bin/renew-certs.sh
One of the trickiest parts of cron work is that jobs run in the background without a terminal. If something goes wrong, you won't see error messages. This is where logging becomes essential.
Always capture output from your cron commands. Append to a log file:
30 2 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1
The >> appends output (rather than overwriting), and 2>&1 captures both standard output and error messages.
Cron can email you when a job runs. Set the MAILTO variable at the top of your crontab:
[email protected]
0 3 * * * /home/user/backup.sh
Now, any output from the backup job will be emailed to that address.
On most Linux systems, cron logs are written to syslog. View them with:
grep CRON /var/log/syslog
Or on systems using journald:
journalctl -u cron
New users often encounter problems with cron. Here are the most common issues:
chmod +x.Cron supports shorthand for common intervals. Instead of remembering the numeric syntax, you can use:
@reboot — at system startup
@yearly — once a year (Jan 1, 00:00)
@monthly — first day of month, 00:00
@weekly — every Sunday, 00:00
@daily — every day at 00:00
@midnight — same as @daily
@hourly — every hour at :00
For example, to run a script every time the system boots:
@reboot /usr/local/bin/startup.sh
System administrators can control who's allowed to use cron. The files /etc/cron.allow and /etc/cron.deny restrict access. If cron.allow exists, only listed users can create cron jobs. If only cron.deny exists, everyone except listed users can use cron.
Most systems don't restrict cron by default, but on shared servers, it's worth checking if you're denied access.
For production servers, you often need system-wide cron jobs that run under a specific user. Edit the system crontab:
sudo nano /etc/cron.d/my-system-jobs
System crontabs require a username field between the time fields and the command:
0 2 * * * root /usr/local/bin/backup.sh
0 */6 * * * www-data /usr/local/bin/clean-cache.sh
The first job runs as root, the second as the www-data user.
Want to extend your Linux automation skills? Check out these related articles on ITVedas:
No. The smallest interval cron supports is one minute. For sub-minute scheduling, you'd need to use a different tool like systemd timers or run a daemon that checks more frequently. However, you can call a script every minute that internally checks millisecond-level timing if needed.
Cron will start another instance of the