Guides

Linux Cron Jobs Explained: Automate Server Tasks Like a Pro

Muhammad SaadAugust 3, 20266 min read
Linux Cron Jobs Explained: Automate Server Tasks Like a Pro

If you're running a website on a VPS, you're probably doing repetitive tasks manually — database backups, log cleanup, SSL renewal checks, cache clearing. A linux cron jobs setup lets you automate all of this, running scripts on a schedule without you lifting a finger. This guide walks you through everything you need to know to set up and manage cron jobs on your server.

What Are Linux Cron Jobs?

Cron is a time-based job scheduler built into every Linux system. It runs commands or scripts at specified intervals — every minute, every hour, daily, weekly, or on custom schedules. The cron daemon (crond) runs in the background and checks every minute whether any scheduled jobs need to execute.

Each user on a Linux system can have their own crontab (cron table) — a file listing their scheduled jobs. The system also has a global crontab for administrative tasks.

Understanding Cron Syntax

Every cron job follows this five-field format:

┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-7, where 0 and 7 = Sunday)
│ │ │ │ │
* * * * * command_to_run

Common Schedule Examples

# Every minute
* * * * * /path/to/script.sh

# Every hour at minute 0
0 * * * * /path/to/script.sh

# Every day at 3:00 AM
0 3 * * * /path/to/script.sh

# Every Monday at 9:00 AM
0 9 * * 1 /path/to/script.sh

# Every 15 minutes
*/15 * * * * /path/to/script.sh

# First day of every month at midnight
0 0 1 * * /path/to/script.sh

# Weekdays at 6:00 PM
0 18 * * 1-5 /path/to/script.sh

Special Shortcuts

Cron also supports readable shortcuts:

@reboot    /path/to/script.sh   # Run once at startup
@hourly    /path/to/script.sh   # Same as 0 * * * *
@daily     /path/to/script.sh   # Same as 0 0 * * *
@weekly    /path/to/script.sh   # Same as 0 0 * * 0
@monthly   /path/to/script.sh   # Same as 0 0 1 * *

Managing Your Linux Cron Jobs

Viewing Current Jobs

# List your cron jobs
crontab -l

# List another user's jobs (requires root)
sudo crontab -u www-data -l

Editing Your Crontab

# Open your crontab in the default editor
crontab -e

# Edit another user's crontab
sudo crontab -u www-data -e

Removing All Jobs

# Delete your entire crontab (use with caution!)
crontab -r

Practical Server Automation Examples

1. Automated Database Backups

# Daily MySQL backup at 2:00 AM
0 2 * * * mysqldump -u dbuser -p'yourpassword' mydb | gzip > /backups/db_$(date +\%Y\%m\%d).sql.gz

# Keep only last 30 days of backups
0 3 * * * find /backups/ -name "db_*.sql.gz" -mtime +30 -delete

2. Log Rotation and Cleanup

# Clear application logs weekly (Sunday 4 AM)
0 4 * * 0 truncate -s 0 /var/www/myapp/storage/logs/*.log

# Archive and compress old logs monthly
0 5 1 * * tar -czf /archives/logs_$(date +\%Y\%m).tar.gz /var/log/nginx/ && find /var/log/nginx/ -name "*.log" -mtime +7 -delete

3. SSL Certificate Renewal

# Check and renew SSL certificates twice daily
0 */12 * * * certbot renew --quiet --post-hook "systemctl reload nginx"

4. WordPress Maintenance

# Run WordPress cron (if wp-cron.php is disabled)
*/15 * * * * cd /var/www/wordpress && php wp-cron.php

# Update WordPress plugins weekly
0 4 * * 1 cd /var/www/wordpress && wp plugin update --all --path=/var/www/wordpress

5. Server Health Monitoring

# Check disk space every hour, alert if over 90%
0 * * * * df -h / | awk 'NR==2 && int($5)>90 {print "DISK ALERT: "$5" used"}' | mail -s "Disk Alert" admin@yourdomain.com

# Restart service if it crashes
*/5 * * * * systemctl is-active --quiet nginx || systemctl restart nginx

Linux Cron Jobs Best Practices

1. Always Use Absolute Paths

Cron runs with a minimal environment. Relative paths and common commands may not work:

# Bad - might fail
0 * * * * backup.sh

# Good - explicit paths
0 * * * * /usr/local/bin/backup.sh

2. Redirect Output to Logs

By default, cron emails output to the system user. Capture it instead:

# Log stdout and stderr
0 3 * * * /path/to/script.sh >> /var/log/myjob.log 2>&1

# Discard output entirely
0 3 * * * /path/to/script.sh > /dev/null 2>&1

3. Use Lock Files to Prevent Overlap

If a job takes longer than expected, the next run might overlap:

# Using flock to prevent concurrent runs
*/5 * * * * flock -n /tmp/myjob.lock /path/to/script.sh

4. Set the PATH Variable

Add a PATH at the top of your crontab:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

0 3 * * * backup.sh  # Now this works

5. Test Before Scheduling

Always test your command manually first:

# Run the exact command you plan to schedule
/usr/local/bin/backup.sh

# Check exit code
echo $?  # Should be 0 for success

Debugging Failed Cron Jobs

When a cron job doesn't run as expected:

# Check cron logs
grep CRON /var/log/syslog

# Check if cron daemon is running
systemctl status cron

# Verify your crontab syntax
crontab -l

# Check mail for error output
cat /var/mail/$(whoami)

Common issues include: wrong file permissions (scripts need execute permission: chmod +x script.sh), missing environment variables, and incorrect paths.

Alternatives to Cron

For more complex scheduling needs:

  • systemd timers — More flexible than cron, with better logging and dependency management
  • at — For one-time scheduled tasks
  • Anacron — For machines that aren't always running (laptops, desktops)
# systemd timer example
# /etc/systemd/system/backup.timer
[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Automate Your Server with DeployBase

Mastering linux cron jobs is essential for any VPS administrator. At DeployBase, our VPS plans give you full root access to set up cron jobs, automated backups, and any server automation you need. With NVMe SSD storage, dedicated resources, and 24/7 support, you can automate with confidence.

Get your VPS at DeployBase → — full control, professional hosting.

Share this article

Muhammad Saad

Muhammad Saad

DeployBase Team

Ready to Get Started?

Join thousands of developers who trust DeployBase for their hosting needs.