A VPS is ideal for scheduled work: it stays online around the clock, unlike a laptop that sleeps. Typical jobs include database backups, report emails, cache warm-ups, data imports, certificate checks and cleanup scripts. On Linux the standard scheduler is cron. It is simple, reliable and preinstalled on Ubuntu and Debian, but a few details trip up almost everyone: a different environment from your shell, silent failures and overlapping runs.
Crontab Syntax Explained
Each job is one line with five time fields followed by the command:
# ┌───────── minute (0-59)
# │ ┌─────── hour (0-23)
# │ │ ┌───── day of month (1-31)
# │ │ │ ┌─── month (1-12)
# │ │ │ │ ┌─ day of week (0-7, Sunday is 0 or 7)
# │ │ │ │ │
* * * * * command-to-run
| Expression | Meaning |
|---|---|
*/5 * * * * | Every 5 minutes |
0 * * * * | At the start of every hour |
30 2 * * * | Every day at 02:30 |
0 9 * * 1-5 | 09:00 on weekdays |
0 0 1 * * | Midnight on the 1st of each month |
15 3 * * 0 | 03:15 every Sunday |
@reboot | Once, when the server boots |
One subtlety: if you restrict both day-of-month and day-of-week, cron runs the job when either matches, not when both do. 0 0 13 * 5 runs on every 13th and on every Friday.
Creating and Editing Jobs
crontab -e # edit your user's crontab
crontab -l # list it
sudo crontab -u www-data -e # edit another user's crontab
Run jobs as the least-privileged user that can do the work. A job that only reads application files should run as the application user, not as root.
System-wide jobs can also go in /etc/cron.d/. Files there use an extra user field:
# /etc/cron.d/app-reports
0 6 * * * appuser /opt/app/bin/send-reports
Simple scripts can be dropped into /etc/cron.daily/, cron.weekly/ or cron.monthly/. They must be executable, and on Debian-based systems the filenames must not contain dots (so backup, not backup.sh), otherwise run-parts skips them.
The Environment Pitfall
Most "works in my terminal, fails in cron" problems come from the environment. Cron runs commands with /bin/sh, a minimal PATH (typically /usr/bin:/bin) and none of your shell's profile settings, virtual environments or exported variables.
- Use absolute paths for every program and file:
/usr/bin/python3, notpython3. - Set variables at the top of the crontab when needed:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
- For Python projects, call the virtual environment's interpreter directly:
/opt/app/venv/bin/python /opt/app/job.py. - Escape percent signs. In a crontab,
%means newline, sodate +%Fmust be writtendate +\%F.
The cleanest approach is to put the real work in a script with its own set -euo pipefail, variables and absolute paths, and keep the crontab line short.
Logging and Catching Failures
By default cron tries to email output to the user. On most VPSs no mail transfer agent is configured, so that output is lost. Redirect both stdout and stderr to a log file:
30 2 * * * /usr/local/bin/backup-db >> /var/log/backup-db.log 2>&1
Check whether cron actually started a job in the system log:
grep CRON /var/log/syslog # Debian/Ubuntu with rsyslog
journalctl -u cron --since today # systemd journal
Rotate your job logs with logrotate so they do not fill the disk. For important jobs, add a heartbeat: have the script ping an uptime monitor (for example a self-hosted Uptime Kuma push monitor) at the end of a successful run, so a missing run raises an alert.
Prevent Overlapping Runs with flock
If a job scheduled every 5 minutes sometimes takes 7, two copies end up running at once. That can corrupt data or exhaust memory. flock (part of util-linux) makes the second run exit instead:
*/5 * * * * /usr/bin/flock -n /tmp/sync.lock /opt/app/bin/sync >> /var/log/sync.log 2>&1
-n means "do not wait; exit if the lock is held". Use a lock file per job.
Time Zones
Cron uses the server's system time zone. Check it with timedatectl. Many VPS images default to UTC, which is 5 hours 30 minutes behind IST. Either set the server to your time zone:
sudo timedatectl set-timezone Asia/Kolkata
sudo systemctl restart cron
or keep UTC (common for servers that serve several regions) and convert your schedules. A job meant for 02:00 IST is 30 20 * * * in UTC on the previous day.
Practical Examples
#!/usr/bin/env bash
# /usr/local/bin/pg-backup (chmod +x)
set -euo pipefail
/usr/bin/pg_dump -U app appdb | /usr/bin/gzip > "/backups/appdb-$(date +%F).sql.gz"
pipefail makes the script fail if pg_dump fails, not just if gzip does. Because date +%F lives in the script, it needs no escaping. Then schedule it:
# Nightly PostgreSQL dump at 01:00
0 1 * * * /usr/local/bin/pg-backup >> /var/log/pgdump.log 2>&1
# Delete backups older than 14 days
30 1 * * * /usr/bin/find /backups -name '*.sql.gz' -mtime +14 -delete
# Laravel scheduler (runs every minute and decides internally what is due)
* * * * * cd /var/www/app && /usr/bin/php artisan schedule:run >> /dev/null 2>&1
# Django management command every 15 minutes
*/15 * * * * /opt/app/venv/bin/python /opt/app/manage.py process_queue >> /var/log/queue.log 2>&1
For the PostgreSQL example, store credentials in ~/.pgpass (mode 600) rather than on the command line, and test restores regularly. A backup you have never restored is not a backup.
Cron vs systemd Timers
systemd timers are the modern alternative and are worth using for important jobs:
| Need | Cron | systemd timer |
|---|---|---|
| Quick, simple schedule | Best fit | More setup (two unit files) |
| Logs | Manual redirection | Automatic in journalctl |
| Run missed jobs after downtime | No (needs anacron) | Persistent=true |
| Resource limits (CPU, memory) | Manual | Built in (MemoryMax=, CPUQuota=) |
| Prevent overlap | flock | Built in (one instance per service) |
A minimal timer pair looks like this:
# /etc/systemd/system/report.service
[Unit]
Description=Generate daily report
[Service]
Type=oneshot
User=appuser
ExecStart=/opt/app/bin/report
# /etc/systemd/system/report.timer
[Unit]
Description=Run report daily
[Timer]
OnCalendar=*-*-* 06:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now report.timer
systemctl list-timers
SkyRainCloud Plans for This Workload
Scheduled jobs such as backups, reports and data syncs rarely need much CPU, but they do need a server that is always on. The entry plans are enough for most cron workloads; choose more RAM if a job processes large files or datasets in memory.
Intel Xeon Platinum 8269CY VPS
KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — Intel | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Business websites, small apps, scheduled jobs and bots | Order |
| VPS 2 — Intel | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,169/month | Business software, mid-size databases, several sites on one server | Check stock |
| VPS 3 — Intel | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,249/month | ERP/CRM stacks, heavier databases, multiple production apps | Check stock |
| VPS 4 — Intel | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,299/month | High-memory workloads and consolidated production servers | Check stock |
AMD EPYC 7C13 VPS
Dedicated KVM vCores on AMD EPYC 7C13 (up to 3.70 GHz) with DDR4 ECC memory, NVMe SSD and an unmetered 1 Gbps port.
| Plan | vCores | RAM | Storage | Network | Price | Good for | Buy |
|---|---|---|---|---|---|---|---|
| VPS 1 — AMD | 2 | 8 GB DDR4 ECC | 40 GB NVMe SSD | 1 Gbps, unmetered | ₹649/month | Websites, small APIs, bots, staging and VPN servers | Order |
| VPS 2 — AMD | 4 | 16 GB DDR4 ECC | 80 GB NVMe SSD | 1 Gbps, unmetered | ₹1,299/month | WooCommerce, Node.js/Python backends, Docker Compose stacks, CI runners | Order |
| VPS 3 — AMD | 8 | 32 GB DDR4 ECC | 150 GB NVMe SSD | 1 Gbps, unmetered | ₹2,499/month | Production databases, high-traffic sites, multi-container apps | Order |
| VPS 4 — AMD | 16 | 64 GB DDR4 ECC | 250 GB NVMe SSD | 1 Gbps, unmetered | ₹4,899/month | Large production workloads, SaaS platforms, analytics, in-memory caches | Order |
Prices are monthly in INR as listed on the SkyRainCloud store (verified 2026-11-14). Stock and final pricing are shown on the product page at checkout.
Troubleshooting Checklist
- Did cron run it? Check
grep CRON /var/log/syslog. No entry usually means a syntax error or the cron service is stopped (systemctl status cron). - Did the command fail? Read your redirected log. Common causes are relative paths, a missing
PATHentry and unescaped%. - Permissions: run the exact command as the cron user with
sudo -u appuser env -i /bin/sh -c '...'to reproduce cron's bare environment. - Script not executable?
chmod +x, and add a shebang line such as#!/usr/bin/env bash. - Wrong time? Check
timedatectl; restart cron after changing the time zone. - Last line ignored? A crontab must end with a newline.
Frequently Asked Questions
How do I run a cron job every 5 minutes?
*/5 * * * * followed by the command, for example */5 * * * * /opt/app/bin/sync >> /var/log/sync.log 2>&1.Why does my script work in the terminal but not in cron?
/bin/sh, a short PATH and none of your profile settings. Use absolute paths, set PATH in the crontab, call your virtualenv's interpreter directly and escape % as \%.Where are cron logs on Ubuntu?
grep CRON /var/log/syslog or journalctl -u cron. The output of your command only goes where you redirect it.How do I stop a cron job from running twice at the same time?
flock: /usr/bin/flock -n /tmp/job.lock /path/to/job. A second run exits immediately while the first holds the lock. systemd timers prevent overlap automatically.What time zone does cron use?
timedatectl. Set it with sudo timedatectl set-timezone Asia/Kolkata and restart cron, or keep UTC and convert your schedule.Should I use cron or systemd timers?
Persistent=true), resource limits or guaranteed non-overlapping runs.How do I run a job when the server starts?
@reboot /path/to/command to the crontab. For long-running services, use a systemd service with Restart=on-failure instead, so it is restarted if it crashes.Can cron run tasks every second?
OnUnitActiveSec= or a long-running worker process.