Hostxpeed
Login Get Started →
Getting Started

How to Set Up Cron Job

7 min read
20 views
Jun 12, 2026

Prerequisites

Before setting up cron jobs, make sure you have:

  • SSH access to your VPS
  • Basic understanding of the command you want to schedule

💡 Cron is a time-based job scheduler that runs commands automatically at specified times.

View Existing Cron Jobs

Connect to your VPS:

ssh hxroot@YOUR_SERVER_IP -p 22

View your crontab:

crontab -l

View another user's crontab:

sudo crontab -u username -l

Edit Cron Jobs

crontab -e

First time, select an editor (nano recommended).

Add jobs using the format:

* * * * * command_to_run

Cron Time Syntax

* * * * * command
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, 0 or 7 = Sunday)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)

Common Examples

Run every minute:

* * * * * /path/to/script.sh

Run at 2:30 AM daily:

30 2 * * * /path/to/script.sh

Run every Sunday at midnight:

0 0 * * 0 /path/to/script.sh

Run on 1st of every month at 3 AM:

0 3 1 * * /path/to/script.sh

Run every 15 minutes:

*/15 * * * * /path/to/script.sh

Run every hour (at minute 0):

0 * * * * /path/to/script.sh

Run at 9:30 AM, Monday to Friday:

30 9 * * 1-5 /path/to/script.sh

Real-World Examples

Daily backup at 1 AM:

0 1 * * * /usr/bin/mysqldump mydb > /backup/mydb.sql

Weekly log cleanup (Sunday 2 AM):

0 2 * * 0 find /var/log -name "*.log" -mtime +30 -delete

Update server every day at 3 AM:

0 3 * * * /usr/bin/apt update && /usr/bin/apt upgrade -y

Check disk usage and email alert:

0 9 * * * df -h | mail -s "Disk Usage" admin@example.com

Special Strings

StringMeaningExample
@rebootRun once at startup@reboot /path/to/script.sh
@dailyOnce a day (0 0 *)@daily /backup/script.sh
@hourlyOnce an hour (0 *)@hourly /update/cache.sh
@weeklyOnce a week (0 0 * * 0)@weekly /cleanup/logs.sh
@monthlyOnce a month (0 0 1 *)@monthly /reports/monthly.sh

Cron Logging and Output

Log output to file:

0 2 * * * /backup/script.sh >> /var/log/backup.log 2>&1

Discard all output:

0 2 * * * /backup/script.sh > /dev/null 2>&1

Email output to admin:

MAILTO=admin@example.com
0 2 * * * /backup/script.sh

Environment in Cron

Cron runs with minimal environment. Set PATH explicitly:

PATH=/usr/local/bin:/usr/bin:/bin
0 2 * * * /usr/local/bin/backup-script

Or set variables in crontab:

MY_VAR=value
0 2 * * * /path/to/script.sh

Testing Cron Jobs

Test every minute temporarily:

* * * * * echo "Test at $(date)" >> /tmp/cron-test.log

Check cron logs:

grep CRON /var/log/syslog

Remove All Cron Jobs

crontab -r

Backup and Restore Crontab

Backup:

crontab -l > my_cron_backup.txt

Restore:

crontab my_cron_backup.txt

Script Example with Error Handling

#!/bin/bash
BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d)
# Check if backup directory exists
if [ ! -d "$BACKUP_DIR" ]; then
    echo "Error: Backup directory not found"
    exit 1
fi
# Perform backup
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" /var/www/
echo "Backup completed at $(date)" >> /var/log/backup.log

Add to crontab:

0 2 * * * /root/backup.sh

✅ You can now schedule automated tasks using cron jobs on your Hostxpeed VPS.

Was this article helpful?