Back to homeCron Generatorvisual scheduler

Guide

How to Use Cron

Learn how to create cron jobs, understand cron syntax, verify your schedules, and debug common cron problems on Linux servers.

What Is Cron?

Cron is a Linux scheduler that runs commands automatically at specific times or intervals. You can use cron jobs for backups, reports, cleanup scripts, emails, imports, and other recurring tasks.

1. Open Crontab

Open your user crontab with this command:

crontab -e

The first time you run it, Linux may ask you to choose an editor. If you are unsure, choose nano.

2. Understand Cron Format

A standard cron job uses five schedule fields followed by the command:

* * * * * command_to_execute
| | | | |
| | | | day of week (0-7, Sunday = 0 or 7)
| | | month (1-12)
| | day of month (1-31)
| hour (0-23)
minute (0-59)

3. Add Cron Job Examples

Run every minute
* * * * * /path/to/script.sh
Run every day at 2:30 AM
30 2 * * * /path/to/script.sh
Run every Sunday at 5 PM
0 17 * * 0 /path/to/script.sh
Run every 5 minutes
*/5 * * * * /path/to/script.sh

4. Make Your Script Executable

If your cron job runs a script, make sure the script has execute permission:

chmod +x /path/to/script.sh

5. Save and Exit

If you are using nano, save and exit with:

CTRL + X, then Y, then Enter

6. Verify Cron Jobs

List installed cron jobs with:

crontab -l

7. Check Cron Logs

On many Linux systems, cron logs can be checked from syslog:

grep CRON /var/log/syslog

Some distributions use different log locations, such as /var/log/cron. Your hosting provider may also expose cron logs in the control panel.

Common Cron Mistakes

Best Practice: Use Full Paths

Cron runs with a minimal environment, so full paths are safer:

/usr/bin/php /var/www/html/script.php

Pro Tip: Log Output and Errors

Redirect both output and errors to a log file:

* * * * * /path/to/script.sh >> /var/log/myscript.log 2>&1

Useful Commands for script.sh

A cron job often points to a shell script such as script.sh. Put your real automation commands inside that script and keep the crontab line simple.

Basic script.sh Template

#!/bin/bash

# Always use full paths in cron scripts
echo "Cron job started at $(date)"

/usr/bin/php /var/www/html/script.php

echo "Cron job finished at $(date)"

Make the script executable:

chmod +x /path/to/script.sh

File Operations

cp /source/file.txt /backup/file.txt
mv /old/file.txt /new/location/
rm /tmp/oldfile.txt
mkdir -p /var/backups/daily

HTTP and API Calls

curl -X GET https://example.com/api
curl -X POST -d "key=value" https://example.com/api

# Download a file
wget https://example.com/file.zip -O /tmp/file.zip

Database Backup with MySQL

Example with password inline:

/usr/bin/mysqldump -u user -p'password' dbname > /backup/db.sql

More secure interactive-style command:

/usr/bin/mysqldump -u user -p dbname > /backup/db.sql

Run PHP, Python, or Node Scripts

/usr/bin/php /var/www/html/cron.php
/usr/bin/python3 /home/user/script.py
/usr/bin/node /home/user/app.js

Zip and Backup Files

tar -czf /backup/site_$(date +\%F).tar.gz /var/www/html

Clean Up Old Files

This deletes files older than 7 days:

find /backup/ -type f -mtime +7 -delete

Logging Output

echo "Process started" >> /var/log/myscript.log

Send Email Notification

echo "Backup completed" | mail -s "Cron Status" you@example.com

Production-Style Backup Script

#!/bin/bash

LOG_FILE="/var/log/backup.log"
DATE=$(date +%F)

echo "[$DATE] Backup started" >> $LOG_FILE

# Backup database
/usr/bin/mysqldump -u root -p'password' mydb > /backup/db_$DATE.sql

# Backup files
tar -czf /backup/files_$DATE.tar.gz /var/www/html

# Cleanup old backups
find /backup/ -type f -mtime +7 -delete

echo "[$DATE] Backup completed" >> $LOG_FILE

Important Script Tips

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

Debug Script for Failed Cron Jobs

Use debug mode and redirect all output to a log file:

#!/bin/bash
set -x

exec >> /var/log/debug.log 2>&1

echo "Starting job"