>
← Back to Linux

Cron Jobs Explained: Schedule Tasks in Linux

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.

What Are Cron Jobs?

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.

Understanding Crontab Files

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 vs System Crontabs

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.

The Cron Syntax Explained

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:

Cron Field Examples

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.

How to Create and Edit Cron Jobs

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.

Viewing Your Crontab

To see what jobs you've already scheduled:

crontab -l

This lists all cron jobs for the current user.

Removing Cron Jobs

To delete all cron jobs for the current user:

crontab -r

To remove just one job, use crontab -e and manually delete that line.

Root Cron Jobs

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.

Practical Cron Job Examples

Daily Database Backup

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.

Weekly Log Rotation

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.

Hourly System Health Check

Run a monitoring script every hour:

0 * * * * /usr/local/bin/health-check.sh

Monthly Certificate Renewal

Renew SSL certificates on the first day of each month at 2 AM:

0 2 1 * * /usr/local/bin/renew-certs.sh

Logging and Debugging Cron Jobs

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.

Redirecting Output

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.

Email Notifications

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.

Checking Cron Logs

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

Common Cron Job Mistakes

New users often encounter problems with cron. Here are the most common issues:

Special Scheduling Strings

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

Managing Cron Permissions

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.

Advanced: System-Wide Cron Jobs

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.

Related Linux Tutorials

Want to extend your Linux automation skills? Check out these related articles on ITVedas:

Frequently Asked Questions

Can cron jobs run at intervals smaller than one minute?

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.

What happens if a cron job is still running when the next scheduled time arrives?

Cron will start another instance of the