Schedule tasks with cron on Linux
Write crontab entries with confidence: the five time fields, working examples, logging output, avoiding overlapping runs and checking that jobs actually ran.
- Beginner
- Linux VPS
- 3 min read
cron runs commands on a schedule: nightly backups, cleanup scripts, report emails or a framework's scheduler. Each user has their own crontab.
1. Make sure cron is installed
It is on Ubuntu and Debian. Minimal AlmaLinux and Rocky images may need it:
sudo dnf install -y cronie
sudo systemctl enable --now crond2. Edit your crontab
crontab -e # your user's jobs
sudo crontab -e # root's jobs
crontab -l # list jobs3. Read the five time fields
# ┌ minute (0-59)
# │ ┌ hour (0-23)
# │ │ ┌ day of month (1-31)
# │ │ │ ┌ month (1-12)
# │ │ │ │ ┌ day of week (0-7, Sunday is 0 or 7)
# │ │ │ │ │
* * * * * commandExamples:
*/5 * * * * /usr/local/bin/check-site.sh # every 5 minutes
30 2 * * * /usr/local/bin/backup.sh # every day at 02:30
0 9 * * 1 /usr/local/bin/weekly-report.sh # Mondays at 09:00
0 0 1 * * /usr/local/bin/monthly-cleanup.sh # first day of each month
@reboot /usr/local/bin/on-boot.sh # once at startupTimes use the server's time zone. Check it with timedatectl, and see Set the time zone.
4. Log the output
By default, output is mailed to the user or lost. Send it to a log file instead, including errors:
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&15. Avoid the three classic mistakes
- PATH is minimal. cron does not load your shell profile. Use full paths (
/usr/bin/php, notphp);command -v phpshows the full path. - `%` is special. In a crontab line,
%means a new line. Escape it as\%, for example$(date +\%F). - Overlapping runs. If a job can take longer than its interval, wrap it in
flockso a second copy never starts while the first runs:
*/5 * * * * /usr/bin/flock -n /tmp/sync.lock /usr/local/bin/sync.shSystem-wide jobs
Files in /etc/cron.d/ have an extra user field after the time fields:
15 3 * * * www-data /usr/bin/php /var/www/example.com/artisan schedule:runTest a script by running it exactly as cron would before you schedule it: sudo -u www-data env -i /bin/sh -c '/usr/local/bin/myjob.sh'. Missing paths or variables show up immediately.
Check that jobs ran
grep CRON /var/log/syslog # Ubuntu, or Debian with rsyslog
journalctl -u cron --since today # Debian 12 / Ubuntu
sudo tail /var/log/cron # AlmaLinux / RockyTroubleshooting
- Job never runs: check the line with
crontab -l, and that the script is executable (chmod +x). - Runs by hand but not in cron: almost always PATH or environment variables. Use full paths and set needed variables at the top of the script.
- Runs at the wrong hour: the server time zone differs from yours, or cron was not restarted after a time zone change.
Next steps
Was this guide helpful?
Still stuck?
Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.
Related guides
Linux VPS
Convert keys between PuTTY (.ppk) and OpenSSH formats
Use PuTTYgen to convert a PuTTY .ppk private key to OpenSSH format and back, understand PPK version 2 and 3, generate a new Ed25519 key and load it in PuTTY.
- Intermediate
- Linux VPS
- 5 min read
Linux VPS
Deploy a Node.js app with PM2 behind Nginx
Install a Node.js LTS release, keep your app running with PM2 across crashes and reboots, and serve it through Nginx with HTTPS.
- Intermediate
- Linux VPS
- 3 min read
Linux VPS
Create an SFTP-only user limited to one folder
Give someone file access over SFTP without a shell, locked inside one folder. Configure internal-sftp with a Match block, set the chroot ownership right and test with WinSCP.
- Advanced
- Linux VPS
- 5 min read