Cron jobs on a Linux server are scheduled tasks that run automatically at fixed times, dates, or intervals. You define tasks in a crontab file using a concise schedule syntax (minute, hour, day, month, weekday) and a command to execute.
This lets you automate backups, updates, monitoring, and routine server maintenance reliably.
If you’re new to automation, learning how to use cron jobs on a Linux server is one of the most valuable admin skills. In this guide, you’ll master crontab basics, schedule syntax, logging, debugging, security, and production-ready examples—based on real-world hosting experience.
What is Cron and How it Works
Cron is a time-based scheduler on Unix/Linux systems. The cron daemon (crond) reads crontab files and runs commands at the specified times. Each user can have a personal crontab, and the system has global cron directories for standardized tasks.

- User crontab: user-specific schedules (via
crontab -e). - System crontab: global schedules (e.g.,
/etc/crontab,/etc/cron.d/). - Cron daemon: continuously monitors schedules and executes commands.
Quick Start: Create Your First Cron Job
Let’s create a simple cron job that writes a timestamp to a log file every 5 minutes.
# Open your user crontab
crontab -e
# Add this line, save and exit:
*/5 * * * * /usr/bin/date >> $HOME/cron-test.log 2>&1
Wait a few minutes, then check the file. If you see timestamps appended, your cron job is running correctly.
Cron Schedule Syntax Explained
Each cron line has five time fields followed by the command. Special strings simplify common schedules.
# ┌───────── minute (0–59)
# │ ┌─────── hour (0–23)
# │ │ ┌───── day of month (1–31)
# │ │ │ ┌─── month (1–12 or names)
# │ │ │ │ ┌─ day of week (0–7 or names; 0/7=Sun)
# │ │ │ │ │
# * * * * * command-to-run
# Special strings:
@reboot run once at startup
@hourly run once an hour
@daily run once a day
@weekly run once a week
@monthly run once a month
@yearly run once a year
- Every 15 minutes:
*/15 * * * * /path/script.sh - Every day at 02:30:
30 2 * * * /path/backup.sh - Mondays at 08:00:
0 8 * * 1 /path/report.sh - First day of month 00:05:
5 0 1 * * /path/monthly.sh - At boot:
@reboot /usr/local/bin/startup-check.sh
Managing Crontab: Edit, List, Remove, and Scope
Use crontab to manage user-specific schedules. System-wide schedules live in files and directories under /etc.
# Edit current user's crontab
crontab -e
# List jobs
crontab -l
# Remove all jobs (careful!)
crontab -r
# Edit another user's crontab (root only)
sudo crontab -u username -e
# System-wide cron files
/etc/crontab
/etc/cron.d/ (individual files)
/etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, /etc/cron.monthly/
On many distributions, /etc/crontab includes a user field. In user crontabs, omit the user field; the job runs as that user.
Environment, PATH, and Permissions
Cron uses a minimal environment. If a command runs fine in your shell but fails in cron, the PATH or environment variables are likely missing.
# Good practice: set the shell, PATH, and crucial env vars at top of crontab
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=you@example.com
# For apps:
# NODE_ENV=production
# APP_HOME=/var/www/app
Always use absolute paths to binaries and scripts. Ensure scripts are executable and owned by the correct user.
# Make script executable and secure
chmod 750 /usr/local/bin/backup.sh
chown root:root /usr/local/bin/backup.sh
# Use absolute paths inside scripts
/usr/bin/mysqldump ... > /var/backups/db.sql
Logging, Debugging, and Monitoring Cron Jobs
By default, cron emails output to MAILTO. In many servers, system logs also record cron events.
- Review logs:
/var/log/cronor/var/log/syslog(distribution-dependent). - Send output to a log file with timestamps.
- Test commands interactively before scheduling.
# Log stdout/stderr to a file with timestamps
*/10 * * * * /usr/local/bin/healthcheck.sh >> /var/log/healthcheck.log 2>&1
# Wrap with a logger
*/30 * * * * /usr/local/bin/task.sh 2>&1 | /usr/bin/logger -t task_cron
# Verify cron service
systemctl status cron # Debian/Ubuntu
systemctl status crond # CentOS/RHEL/AlmaLinux/Rocky
If a job doesn’t fire, check time, time zone, permissions, SELinux/AppArmor, and whether the script requires a TTY or interactive shell.
Common Cron Job Examples You Can Copy
Below are production-friendly examples for typical server tasks. Adjust paths and users to your environment.
# 1) Rotate and compress logs daily at 00:10
10 0 * * * /usr/sbin/logrotate -f /etc/logrotate.conf
# 2) MySQL database backup every night at 02:00
0 2 * * * /usr/bin/mysqldump -u backup -p'strongPass' --single-transaction --quick --lock-tables=false dbname | /usr/bin/gzip > /var/backups/dbname-$(date +\%F).sql.gz
# 3) Sync files to remote storage at 03:15
15 3 * * * /usr/bin/rsync -az /var/www/ user@backup.example:/data/www/
# 4) Run a WordPress cron alternative every 5 minutes
*/5 * * * * /usr/bin/php -d detect_unicode=0 /var/www/html/wp-cron.php >> /var/log/wp-cron.log 2>&1
# 5) Renew Let's Encrypt certificates twice daily
0 3,15 * * * /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx"
For WordPress sites, consider disabling the internal pseudo-cron (DISABLE_WP_CRON) and trigger wp-cron.php via system cron for consistent execution under load.
Security Best Practices for Cron
- Least privilege: run jobs as the minimal-privilege user; avoid root unless necessary.
- Validate scripts: store in secured directories with restricted permissions.
- Use absolute paths to prevent PATH injection risks.
- Escape secrets; prefer env files or a secret manager over inline passwords.
- Restrict cron usage: manage
/etc/cron.allowand/etc/cron.deny. - Log and alert: forward logs to a SIEM or alerting system.
Cron vs. Systemd Timers vs. Anacron
Cron is universal and simple. Systemd timers add dependency management, missed-run handling, and richer logging, while Anacron ensures periodic jobs run even if the system was off at the scheduled time.
- Use cron for precise, lightweight schedules.
- Use systemd timers for complex orchestration and reliable catch-up.
- Use Anacron for laptops/instances that aren’t always on.
# Example: run script every 15 minutes with systemd timer
# /etc/systemd/system/report.service
[Unit]
Description=Generate report
[Service]
Type=oneshot
ExecStart=/usr/local/bin/report.sh
# /etc/systemd/system/report.timer
[Unit]
Description=Run report every 15 minutes
[Timer]
OnBootSec=2m
OnUnitActiveSec=15m
Unit=report.service
[Install]
WantedBy=timers.target
# Enable
systemctl enable --now report.timer
Cron on Shared Hosting and With YouStable
On shared hosting with cPanel or similar, you can configure cron jobs from the control panel. The interface helps set schedules, command paths, and email notifications without touching the shell.
At YouStable, our hosting plans support cron via UI and SSH. For busy WordPress or WooCommerce sites, our team can help convert WP-Cron to system cron, tune PHP CLI settings, and configure reliable logs—so scheduled tasks run on time even under heavy traffic.
Troubleshooting Checklist
- Is
cron/crondrunning? Check withsystemctl. - Does your schedule match your time zone? Verify with
date. - Are absolute paths used for binaries and files?
- Is the script executable and owned by the correct user?
- Are needed environment variables set in the crontab?
- Any output or errors in logs or emails (
MAILTO)? - SELinux/AppArmor blocking? Review audit logs or set proper contexts.
- Does the command require a TTY or interactive input? If so, refactor.
Real-World Tips from Hosting Experience
- Batch heavy jobs during off-peak hours to reduce load spikes.
- Stagger related jobs by a few minutes to avoid contention.
- Throttle with
flockto prevent overlapping runs on slow servers. - Version control your scripts and keep a change log tied to cron edits.
- Test with the same user that will run the cron job (env differences matter).
# Use flock to prevent overlapping jobs
*/10 * * * * /usr/bin/flock -n /var/run/report.lock /usr/local/bin/report.sh
FAQs:
How do I list all cron jobs for a user?
Run crontab -l as that user to view their crontab. As root, use crontab -u username -l. System-wide jobs can be found in /etc/crontab, /etc/cron.d/, and periodic directories like /etc/cron.daily/.
How do I run a cron job every minute?
Use * * * * * as the schedule. Example: * * * * * /usr/local/bin/task.sh >> /var/log/task.log 2>&1. Ensure the script is executable and uses absolute paths.
Where are cron logs stored?
On Debian/Ubuntu, check /var/log/syslog for cron entries; on RHEL/CentOS/AlmaLinux/Rocky, check /var/log/cron. You can also redirect job output to dedicated log files for easier debugging.
What’s the difference between user crontab and /etc/crontab?
User crontabs run as the owning user and omit the user field. /etc/crontab is system-wide and includes a user field for each job, letting you specify which user executes the command.
Why does my cron job run manually but not in cron?
Usually due to environment differences: missing PATH, required variables, permissions, or relative paths. Set SHELL and PATH in the crontab, use absolute paths, and redirect output to logs to capture errors.