Cron jobs on a Linux server are automated, time based tasks managed by the cron daemon (crond). They run commands or scripts at scheduled intervals—every minute, hour, day, week, or on custom schedules—using crontab files. Cron is ideal for backups, reports, cache refreshes, SSL renewals, and routine server maintenance.
If you manage websites or applications, understanding Cron Jobs on Linux Server is essential. Cron automates repetitive tasks, reduces human error, and keeps systems healthy. In this guide, I’ll explain cron from the ground up and share best practices we apply at YouStable for reliable, secure scheduling.
What is Cron and How It Works?

Cron is a background service that checks a set of schedules once per minute and runs commands when they match the current time. Those schedules live in crontab files per user or system-wide. Each entry specifies when to run and what to execute.
Cron Components You Should Know
– crond: The cron daemon process that evaluates schedules and runs jobs.
– User crontabs: Per-user schedules edited with crontab -e. Stored under /var/spool/cron/ or /var/spool/cron/crontabs/.
– System crontabs: Files such as /etc/crontab and /etc/cron.d/, plus periodic directories /etc/cron.hourly, daily, weekly, monthly.
– anacron: Ensures daily/weekly/monthly jobs run even if the machine was off at the scheduled time (useful for laptops or non-24/7 servers).
– systemd timers: A modern alternative to cron on systemd-based distros, offering more features like dependencies and logging.
Cron Syntax Explained (with Examples)
A standard cron expression has five time fields followed by a command:
# ┌──────── minute (0 - 59)
# │ ┌────── hour (0 - 23)
# │ │ ┌──── day of month (1 - 31)
# │ │ │ ┌── month (1 - 12 or JAN-DEC)
# │ │ │ │ ┌ day of week (0 - 7; 0 or 7 = Sunday or SUN-SAT)
# │ │ │ │ │
# * * * * * command to run
Special characters:-
- * = any value
- , = multiple values (e.g., 1,15,30)
- – = range (e.g., 9-17)
- / = step (e.g., */5 every 5 units)
Descriptors (shortcuts): @reboot, @hourly, @daily, @weekly, @monthly, @yearly, @annually.
Common Cron Schedule Examples
# Every 5 minutes
*/5 * * * * /usr/local/bin/script.sh
# At 2:30 AM daily
30 2 * * * /usr/local/bin/backup.sh
# Weekdays at 9 AM
0 9 * * 1-5 /usr/local/bin/report.sh
# On the 1st and 15th at midnight
0 0 1,15 * * /usr/local/bin/cleanup.sh
# At reboot
@reboot /usr/local/bin/startup-check.sh
Create, Edit, and Manage Cron Jobs
Use the crontab command to manage per-user cron jobs.
This is the safest and most common method for application tasks.
# Edit your crontab (opens your default editor)
crontab -e
# List your cron jobs
crontab -l
# Remove your crontab
crontab -r
# Edit root's crontab (requires sudo)
sudo crontab -e
Environment Variables in Cron
Cron runs with a minimal environment.
Define what you need at the top of your crontab:
MAILTO=admin@example.com
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin
CRON_TZ=UTC
Always use absolute paths to binaries and files. Don’t assume your interactive shell configuration applies to cron.
Typical Use Cases
- Database and file backups
- Log rotation or cleanup
- Regenerating sitemaps and reports
- Refreshing caches or queues
- SSL certificate renewals (e.g., certbot)
# MySQL dump at 02:00 daily
0 2 * * * /usr/bin/mysqldump -u backup -p'strongpass' dbname | gzip > /backups/db-$(date +\%F).sql.gz
# Let's Encrypt renewal twice daily and reload Nginx if changed
0 */12 * * * /usr/bin/certbot renew --quiet --deploy-hook "systemctl reload nginx"
Logging, Monitoring, and Testing Cron Jobs
By default, cron captures command output and can email it to MAILTO. You can also redirect output to log files for troubleshooting.
# Log stdout and stderr with timestamps
*/10 * * * * /usr/local/bin/task.sh >> /var/log/task.log 2>&1
# Use syslog via logger
*/15 * * * * /usr/local/bin/check.sh 2>&1 | /usr/bin/logger -t check-cron
Where to read cron logs depends on your distro:
- Debian/Ubuntu:
/var/log/syslog - RHEL/CentOS/Alma/Rocky:
/var/log/cron
# Check service and tail logs
systemctl status cron # Debian/Ubuntu
systemctl status crond # RHEL/CentOS
tail -f /var/log/syslog # Debian/Ubuntu
tail -f /var/log/cron # RHEL/CentOS
Test your command manually first, then add it to cron. If it works interactively but not in cron, it’s almost always an environment or path issue.
Security, Permissions, and Reliability
- Least privilege: Put jobs in the owning user’s crontab. Use root only when absolutely necessary.
- Restrict access: Manage who can use cron with
/etc/cron.allowand/etc/cron.deny. - Executable and shebang: Ensure scripts are executable and start with the correct interpreter (e.g.,
#!/usr/bin/env bash). - Absolute paths: Use full paths to binaries, files, and virtualenvs.
- Quoting and spaces: Quote variables and paths with spaces to avoid breakage.
- Concurrency control: Use
flockto prevent overlapping runs.
# Prevent overlap with flock (creates/locks a file)
* * * * * /usr/bin/flock -n /var/lock/myjob.lock /usr/local/bin/myjob.sh
If your system is not always on, install and configure anacron to “catch up” missed daily/weekly/monthly tasks. For complex dependencies, consider systemd timers.
Common Cron Errors and How to Fix Them
- Command not found: Set
PATHin crontab or use absolute paths (e.g.,/usr/bin/python3). - Permission denied: Make scripts executable (
chmod +x) and check file permissions/ownership. - Different environment: Cron doesn’t read your shell rc files; set
HOME,PATH, and other vars explicitly. - CRLF line endings: Convert Windows line endings with
dos2unix. - SELinux/AppArmor blocks: Review audit logs and adjust policy or context if needed.
- Relative paths: Use absolute paths for reads/writes (e.g.,
/var/www/app). - Virtualenvs: Activate in-line (e.g.,
source /path/venv/bin/activate && python script.py) or call full interpreter path.
# Example robust cron entry with environment and logging
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/bin
MAILTO=ops@example.com
0 1 * * * /usr/bin/flock -n /var/lock/report.lock /usr/bin/python3 /opt/app/report.py --quiet >> /var/log/report.log 2>&1
Website Owner Playbook: Practical Cron Jobs
- WordPress cron replacement: Disable pseudo-cron and schedule real cron for reliability.
- Regular backups: Database and uploads to local or remote storage.
- Search index or sitemap refresh: Improve SEO consistency.
- Cache warmers and queue workers: Reduce first-hit latency.
- Security scans: Run ClamAV or malware scanners during off-peak hours.
# Disable WP pseudo-cron in wp-config.php:
# define('DISABLE_WP_CRON', true);
# Run wp-cron.php every 5 minutes via system cron:
*/5 * * * * /usr/bin/php -f /var/www/html/wp-cron.php >> /var/log/wp-cron.log 2>&1
# Nightly WordPress file backup
30 1 * * * /usr/bin/rsync -a --delete /var/www/html/ /backups/wp-files/
# Daily DB backup with 7-day retention
0 2 * * * /usr/bin/mysqldump -u wp -p'secret' wpdb | gzip > /backups/wpdb-$(date +\%F).sql.gz; find /backups -name "wpdb-*.sql.gz" -mtime +7 -delete
Cron vs. systemd Timers vs. Anacron
When to Use Cron
- Simple, time-based tasks without complex dependencies.
- Works across virtually all Linux distributions.
- Lightweight and easy to audit with a single crontab.
When to Use systemd Timers
- Need for service dependencies, restart policies, and centralized journal logging.
- Better monitoring with
systemctlandjournalctl. - Calendar expressions like
OnCalendar=Mon..Fri 09:00.
When to Use Anacron
- Systems that are not always on.
- Guarantee daily/weekly/monthly jobs still run if missed.
Best Practices Checklist
- Document every job (who, what, why, when).
- Keep scripts in version control and deploy via CI/CD.
- Centralize logs and alerts (email, Slack, syslog, or monitoring).
- Use
flockto prevent overlaps andnice/ioniceto reduce resource contention. - Set
PATH,MAILTO, and timezone explicitly when needed. - Review crontabs during releases and after OS updates.
- Test manually, then test under cron with the same environment.
How We Handle Cron at YouStable
On YouStable managed VPS and cloud servers, we configure cron with proper environment, logging, and access controls. We add safeguards like flock, structured logs, and monitoring hooks, and we recommend systemd timers where they fit better. If you need help auditing or migrating cron jobs, our team can assist end-to-end.
FAQ’s – Cron Jobs on Linux Server
Where are cron logs stored on Linux?
On Debian/Ubuntu, cron messages go to /var/log/syslog. On RHEL/CentOS/Alma/Rocky, check /var/log/cron. You can also pipe output to files or use logger for structured syslog entries.
How do I run a cron job every 5 minutes?
Use a step in the minute field: */5 * * * * /path/to/command. This runs at minute 0,5,10,…,55 every hour.
Does cron use my server timezone?
Yes, cron uses the system timezone. You can override per crontab with CRON_TZ=Region/City. Verify with timedatectl and ensure consistent settings across servers.
Will cron run jobs if the server was down at schedule time?
Standard cron does not run missed jobs. Use anacron for daily/weekly/monthly catch-up, or switch to systemd timers with Persistent=true for catch-up behavior.
How do I replace WordPress’s pseudo-cron with real cron?
Set define('DISABLE_WP_CRON', true); in wp-config.php, then add a system cron: */5 * * * * /usr/bin/php -f /var/www/html/wp-cron.php. This improves reliability and performance, especially on busy sites.
Cron is simple, fast, and battle-tested. With clear schedules, proper logging, and a few safeguards, you can automate your Linux server confidently. If you want expert help deploying production-grade schedules, YouStable’s managed hosting team is ready to assist.