01
How it works: cron and the crontab
cron is a service that is almost always already installed on Ubuntu and Debian (if not: sudo apt install cron). Every minute, it reads lists of tasks, the crontabs, and runs those whose time has come. Each user has their own crontab; commands in it run with that user's rights.
Once connected over SSH (see connecting to a VPS over SSH), three commands are enough:
crontab -e
crontab -l
sudo crontab -e
crontab -e opens your crontab in an editor. The first time, Ubuntu may ask you to choose the editor: nano is the simplest (Ctrl+O to save, Ctrl+X to exit). When you save, the crontab is installed and taken into account without restarting anything.
crontab -l shows its content.
sudo crontab -e edits the root crontab, for tasks that need administrator rights (backing up the whole system, for example).
Watch out for crontab -r, right next to -e on the keyboard: it deletes the whole crontab, without confirmation. Keep a copy of your lines (crontab -l > ~/crontab.sauvegarde).
02
The syntax: five fields, then the command
Each active line of a crontab holds five date and time fields, separated by spaces, followed by the command to run:
# minute hour day-of-month month day-of-week command
30 3 * * * /usr/local/bin/mon-script.sh
- minute: 0 to 59;
- hour: 0 to 23;
- day of month: 1 to 31;
- month: 1 to 12, or the first three letters (
jan, feb…);
- day of week: 0 to 7, where 0 and 7 both mean Sunday, or
mon, tue…
An asterisk * means "every value". You can also write a range (1-5), a list (1,15) or a step (*/10, every 10 units). A few examples:
*/5 * * * * every 5 minutes
0 * * * * every hour, on the hour
30 3 * * * every day at 3:30 am
0 4 * * 1 every Monday at 4 am
0 2 1 * * on the 1st of each month at 2 am
0 9-18 * * 1-5 every hour from 9 am to 6 pm, Monday to Friday
@reboot once, when the cron service starts
One subtlety: if the day of month and the day of week are both set, the task runs when either one or the other matches. 0 4 1 * 1 runs on the 1st of the month and also every Monday.
To check an expression before using it, the site crontab.guru turns it into a sentence and lists the next runs. Times follow the VPS time zone: the timedatectl command shows it, and sudo timedatectl set-timezone Europe/Paris changes it.
03
Absolute paths and an output log
Most tasks that "do not work under cron" while they work by hand have one of these three causes.
1. The environment is reduced. cron runs commands with /bin/sh and a PATH (the list of folders where programs are looked up) much shorter than in your session. So write absolute paths, for programs as well as for files. The which command gives the full path of a program:
which node
/usr/bin/node
In a script, do the same or set a full PATH at the top. Your session's settings (.bashrc, aliases) are not loaded.
2. The % sign has a special meaning. In a crontab line, a % not preceded by a backslash is turned into a newline. Write date +\%F, or better, put the command in a script, where % is written normally.
3. The output is lost. Whatever the command prints is emailed to the crontab's owner; with no mail server on the VPS, it goes nowhere, error messages included. Redirect the output to a log file:
*/10 * * * * /usr/local/bin/verifier-site.sh >> /var/log/verifier-site.log 2>&1
>> appends normal output to the end of the file (> would overwrite it each time), and 2>&1 sends errors to the same place. The file must be writable by the crontab's user: for an ordinary user's task, pick a file in their home folder.
To check that cron did start the task, read its own log:
sudo journalctl -u cron --since today
04
Example: a backup every night
Here is a simple backup of a site stored in /var/www/monsite, to adapt to your folders. Create the script:
sudo nano /usr/local/bin/sauvegarde-site.sh
#!/bin/sh
set -e
DEST=/var/backups/site
mkdir -p "$DEST"
tar czf "$DEST/site-$(date +%F).tar.gz" -C /var/www monsite
find "$DEST" -name 'site-*.tar.gz' -mtime +7 -delete
set -e stops the script at the first error;
tar czf creates a compressed archive, dated with date +%F (in the 2026-12-03 format);
find … -mtime +7 -delete removes archives older than 7 days, so the disk does not fill up.
Make it executable, test it once by hand, then schedule it in the root crontab (sudo crontab -e):
sudo chmod +x /usr/local/bin/sauvegarde-site.sh
sudo /usr/local/bin/sauvegarde-site.sh
ls -lh /var/backups/site
30 3 * * * /usr/local/bin/sauvegarde-site.sh >> /var/log/sauvegarde-site.log 2>&1
A backup that stays on the VPS disappears with it: regularly copy the archives elsewhere, with rsync or scp to another machine, or download them over SFTP (see transferring files with FileZilla or WinSCP). For a database, add to the script the export tool provided by its developer (for example mysqldump for MySQL and MariaDB) rather than copying its files while it runs.
05
The alternative: systemd timers
Ubuntu also offers systemd timers. They need two files instead of one line, but the output goes straight to the journal, a run missed while the VPS was off can be caught up, and a task still running is not started again on top of itself. The same example, in two files:
# /etc/systemd/system/sauvegarde-site.service
[Unit]
Description=Site backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/sauvegarde-site.sh
# /etc/systemd/system/sauvegarde-site.timer
[Unit]
Description=Nightly site backup
[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true
[Install]
WantedBy=timers.target
The timer starts the service with the same name. OnCalendar sets the time (here every day at 3:30 am; daily means every day at midnight), and Persistent=true runs the task at boot if a run was missed. Enable it and check:
sudo systemctl daemon-reload
sudo systemctl enable --now sauvegarde-site.timer
systemctl list-timers
systemd-analyze calendar "*-*-* 03:30:00"
sudo journalctl -u sauvegarde-site.service
systemctl list-timers shows the next run of each timer, systemd-analyze calendar checks an expression. Do not keep the same task in both cron and a timer: it would run twice. To then keep an eye on the disk your backups fill, see monitoring VPS resources.
06
On CloudFive
On our Linux VPS, you have root access to set up cron and systemd timers as you see fit. The Ryzen VPS, from €5.99 per month, runs on AMD Ryzen 9 9950X with DDR5 memory and NVMe storage; an Intel Xeon range exists too.
- Snapshots from the customer area, to take before changing a script that touches your data. They do not replace a backup copied off the VPS.
- A console in the customer area, a dedicated IPv4 address and basic anti-DDoS protection included.
- VPS in KVM or as an LXC container: scheduled tasks work the same way in both.
The servers are in France, monthly with no commitment, and support answers in French 7 days a week by ticket, on Discord and by phone. Before automating, apply the basic settings described in securing a Linux VPS and updating Ubuntu.
07
Frequently asked questions
How do I add a cron job on a VPS?
Connect over SSH, type crontab -e, then add a line with five fields (minute, hour, day of month, month, day of week) followed by the command. For example, 30 3 * * * /usr/local/bin/script.sh runs the script every day at 3:30 am. Save: the task is taken into account right away.
Why is my cron job not working?
Most often, a relative path or a program that cannot be found: cron uses a reduced environment, with a short PATH. Write absolute paths, escape % signs in the crontab line, and redirect output to a file with >> file.log 2>&1 to read the error.
How do I run a cron job every 5 minutes?
Write */5 in the minute field and asterisks in the other four: */5 * * * * followed by the command. The same principle gives */15 for every 15 minutes, or 0 */2 * * * for every two hours.
Where can I see whether cron ran my task?
On Ubuntu, sudo journalctl -u cron shows each command cron starts. To see what the command printed or its errors, redirect its output to a log file in the crontab line, then read that file.
Should I use cron or a systemd timer?
cron is enough for most tasks and fits on one line. A systemd timer needs two files, but logs the output to the journal, can catch up a missed run with Persistent=true and does not start a task again while it is still running.