What I Wanted to Do

I wanted to run scripts automatically on a schedule on Linux. With cron, you can set commands to execute automatically at specified times.

Environment

  • Linux (Ubuntu / Debian)
  • WSL2

Cron Basics

Editing crontab

crontab -e    # Edit the current user's crontab
crontab -l    # Display current settings
crontab -r    # Delete crontab

Cron expression syntax

minute hour day month weekday command
*      *    *   *     *

Common configuration examples

# Backup every day at 2 AM
0 2 * * * /home/user/backup.sh

# Run every Monday at 9 AM
0 9 * * 1 /home/user/weekly.sh

# Run at minute 0 of every hour
0 * * * * /home/user/hourly.sh

# Run every 5 minutes
*/5 * * * * /home/user/check.sh

# Run on the 1st of every month at midnight
0 0 1 * * /home/user/monthly.sh

Real Configuration Example

crontab -e

When the editor opens, add the following:

# Delete log files daily
0 3 * * * find /var/log/myapp -name "*.log" -mtime +7 -delete

# Run script every minute and log the output
* * * * * /home/user/script.sh >> /var/log/cron.log 2>&1

Checking Cron Logs

grep CRON /var/log/syslog
tail -f /var/log/cron.log

Common Pitfalls

  • Specify commands with their full path in cron (e.g. /usr/bin/python3)
  • Environment variables are not inherited in cron
  • Adding 2>&1 also captures error output in logs
  • */5 means “every multiple of 5 minutes”

To check cron logs in real time, use How to Monitor Logs in Real Time with tail -f on Linux.

Looking for reliable cloud infrastructure? Check out these developer-friendly services.