Server Administration12 min read•Updated November 2026

How to Schedule Tasks on a Linux VPS with Cron

Quick answer

Cron runs commands on a schedule. Edit your jobs with crontab -e and add lines in the form minute hour day-of-month month day-of-week command; for example 30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1 runs every day at 02:30. Always use absolute paths, redirect output to a log, and wrap long jobs in flock so runs never overlap.

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
ExpressionMeaning
*/5 * * * *Every 5 minutes
0 * * * *At the start of every hour
30 2 * * *Every day at 02:30
0 9 * * 1-509:00 on weekdays
0 0 1 * *Midnight on the 1st of each month
15 3 * * 003:15 every Sunday
@rebootOnce, 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, not python3.
  • 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, so date +%F must be written date +\%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:

NeedCronsystemd timer
Quick, simple scheduleBest fitMore setup (two unit files)
LogsManual redirectionAutomatic in journalctl
Run missed jobs after downtimeNo (needs anacron)Persistent=true
Resource limits (CPU, memory)ManualBuilt in (MemoryMax=, CPUQuota=)
Prevent overlapflockBuilt 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.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — Intel28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthBusiness websites, small apps, scheduled jobs and botsOrder
VPS 2 — Intel416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
VPS 3 — Intel832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
VPS 4 — Intel1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,299/monthHigh-memory workloads and consolidated production serversCheck 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.

PlanvCoresRAMStorageNetworkPriceGood forBuy
VPS 1 — AMD28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹649/monthWebsites, small APIs, bots, staging and VPN serversOrder
VPS 2 — AMD416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,299/monthWooCommerce, Node.js/Python backends, Docker Compose stacks, CI runnersOrder
VPS 3 — AMD832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,499/monthProduction databases, high-traffic sites, multi-container appsOrder
VPS 4 — AMD1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,899/monthLarge production workloads, SaaS platforms, analytics, in-memory cachesOrder

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

  1. 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).
  2. Did the command fail? Read your redirected log. Common causes are relative paths, a missing PATH entry and unescaped %.
  3. Permissions: run the exact command as the cron user with sudo -u appuser env -i /bin/sh -c '...' to reproduce cron's bare environment.
  4. Script not executable? chmod +x, and add a shebang line such as #!/usr/bin/env bash.
  5. Wrong time? Check timedatectl; restart cron after changing the time zone.
  6. Last line ignored? A crontab must end with a newline.

Frequently Asked Questions

How do I run a cron job every 5 minutes?
Use */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?
Cron uses a minimal environment: /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?
Cron records job starts in the system log. Use 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?
Wrap it in 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?
The server's system time zone, shown by 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?
Cron is fine for simple jobs. Prefer systemd timers when you want automatic logging in the journal, catch-up after downtime (Persistent=true), resource limits or guaranteed non-overlapping runs.
How do I run a job when the server starts?
Add @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?
No, the smallest cron interval is one minute. For sub-minute schedules, use a systemd timer with OnUnitActiveSec= or a long-running worker process.
Is it safe to run cron jobs as root?
Only when the job truly needs root, such as some system maintenance. Run application jobs as the application user to limit the damage a buggy or compromised script can cause.
Which SkyRainCloud plan is best for scheduled scripts?
Most cron workloads run comfortably on the entry 2 vCore / 8 GB VPS. Choose 4 vCores / 16 GB or more if jobs process large datasets, run in parallel, or share the server with a busy application.
Instant UPI • ₹649/mo
Fast KVM Cloud Provisioning