Your First Cron Job
Step-by-step tutorial to create and run your first automated task.
Tutorial Goal: Create a simple cron job that writes a timestamp to a file every minute, then learn how to manage and modify it.
Prerequisites
Before You Start
- ✓ Cron is installed and running (see Installation Guide)
- ✓ You have terminal/command line access
- ✓ Basic understanding of file paths
- ✓ Text editor knowledge (nano, vim, or any editor)
Step 1: Access Your Crontab
Each user has their own crontab (cron table). Let's open yours for editing:
# Open your personal crontab for editing
crontab -e
First Time? You might be asked to choose an editor. We recommend nano for beginners as it's the most user-friendly.
Step 2: Create a Simple Job
Add this line to your crontab. It will write the current date and time to a file every minute:
# Write timestamp every minute
* * * * * echo "$(date): Hello from cron!" >> /tmp/my-first-cron.log
Breaking Down the Command:
* * * * *- Run every minuteecho- Command to print text$(date)- Get current date/time>>- Append to file (don't overwrite)/tmp/my-first-cron.log- Output file location
Step 3: Save and Exit
Using Nano Editor
- Press Ctrl + O to save
- Press Enter to confirm filename
- Press Ctrl + X to exit
Using Vim Editor
- Press Esc to enter command mode
- Type :wq and press Enter
You should see a message like "crontab: installing new crontab" confirming the job was saved.
Step 4: Verify Your Job
Check that your cron job was saved correctly:
# List your current cron jobs
crontab -l
You should see your job listed. Now wait a minute or two and check if it's working:
# Check if the job is creating output
cat /tmp/my-first-cron.log
# Watch the file update in real-time
tail -f /tmp/my-first-cron.log
🎉 Success!
If you see timestamps appearing every minute, congratulations! Your first cron job is working perfectly.
Step 5: Modify the Schedule
Let's change it to run every 5 minutes instead. Edit your crontab again:
crontab -e
Change the first field from * to */5:
# Run every 5 minutes instead
*/5 * * * * echo "$(date): Hello every 5 minutes!" >> /tmp/my-first-cron.log
Step 6: Clean Up
When you're done testing, remove the job to avoid filling up your log file:
# Remove all cron jobs for current user
crontab -r
# Or edit and delete just this job
crontab -e
# Clean up the test file
rm /tmp/my-first-cron.log
Common Gotchas for Beginners
⚠️ Path Issues
Cron runs with minimal environment. Always use full paths:
❌ Bad: echo "test" >> mylog.txt✅ Good: echo "test" >> /home/user/mylog.txt
⚠️ Environment Variables
Cron doesn't load your shell profile. Set PATH if needed:
PATH=/usr/local/bin:/usr/bin:/bin
⚠️ Permissions
Make sure your user can write to the target location and execute the commands.
More Practical Examples
Daily Backup at 2 AM
0 2 * * * tar -czf /backups/daily-$(date +\%Y\%m\%d).tar.gz /home/user/documents
Weekly System Update (Sundays 3 AM)
0 3 * * 0 /usr/bin/apt update && /usr/bin/apt upgrade -y
Cleanup Temp Files (Every Hour)
0 * * * * find /tmp -type f -mtime +1 -delete
🚀 You Did It!
Congratulations! You've successfully created, modified, and cleaned up your first cron job. Ready to learn more?
Ready-to-Use Examples
Now that you know the basics, try these real-world patterns: