Is cron running on my machine?
Check before you write anything. The package and service names differ by family:
| System | Package | Service |
|---|---|---|
| Ubuntu, Debian | cron | systemctl status cron |
| Enterprise Linux family | cronie | systemctl status crond |
| FreeBSD 14 | base system | service cron status |
If the package is missing on a minimal install, add it with apt install cron or dnf install cronie and enable it with systemctl enable --now cron or crond.
Where should the job go?
User jobs go in that user's crontab, edited with crontab -e. System jobs that belong to a package or a service are clearer as a file in /etc/cron.d/, which adds a user column so each line says who runs it:
# /etc/cron.d/site-maintenance (file mode 644, owned by root, no dots in the name) SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin MAILTO="" 12 4 * * * www-data /usr/local/bin/purge-thumbs.sh */10 * * * * root /usr/local/sbin/check-disk.sh
On Debian and Ubuntu, files in /etc/cron.d are ignored if their names contain a dot, so site-maintenance works while site.cron does not. The timing fields are explained in Cron expressions.
Why does my job work by hand but not from cron?
Because cron runs with a nearly empty environment. Your login shell sets a long PATH, a language, perhaps a virtualenv or an nvm version; cron gives you /usr/bin:/bin and little else, and runs commands with /bin/sh, not bash. Three fixes cover almost every case:
- Use full paths to every program, or set
PATHat the top of the crontab. - Put the work in a script with a proper shebang such as
#!/usr/bin/env bashandset -euo pipefail. cdto the working folder inside the script rather than assuming one.
To reproduce cron's environment for testing, run env -i SHELL=/bin/sh PATH=/usr/bin:/bin HOME=$HOME /usr/local/bin/purge-thumbs.sh. Whatever fails there will fail under cron.
How do I capture output and errors?
By default cron tries to email output, and on most servers no mail system is set up, so it vanishes. Send it somewhere visible. Either append to a log file, or hand it to the journal with systemd-cat:
30 1 * * * /usr/local/bin/export-orders.sh >> /var/log/export-orders.log 2>&1 30 1 * * * systemd-cat -t export-orders /usr/local/bin/export-orders.sh
With the second form, journalctl -t export-orders --since yesterday shows every run with timestamps, and the journal handles rotation for you. For log files, add a small logrotate rule so they do not grow forever.
How do I stop runs from overlapping?
Wrap the command in flock. If yesterday's run is still busy, today's exits quietly instead of starting a second copy that fights over the same files:
*/15 * * * * /usr/bin/flock -n /run/lock/sync-feeds.lock /usr/local/bin/sync-feeds.sh
-n means do not wait; use -w 120 instead to wait up to two minutes. This one habit prevents a whole class of mysterious slowdowns on busy machines.
How do I know when a job fails?
Make silence mean success. Have the script exit non-zero on errors, then alert from the wrapper: post to a chat webhook with curl, or ping a dead man's switch service at the end of each successful run so it can warn you when the ping stops arriving. Self-hosted options such as Healthchecks run nicely on a small second machine like E1, which also keeps monitoring outside the box it watches.
Should I use a systemd timer instead?
For important jobs, often yes. Timers log to the journal automatically, can catch up runs missed during a reboot with Persistent=true, can add random delay to spread load, and can depend on other units such as the network or a mounted drive. Cron is simpler for quick one-liners and familiar to everyone. Both are present on every Linux image we install, so pick per job. systemctl list-timers shows the timers already active, and List all cron jobs shows how to find every cron job on a machine.
Keep Reading
Cron expressions
Read and write the five cron fields, steps, ranges, lists and shortcuts, plus the day-of-month and weekday trap that surprises everyone.
OpenList all cron jobs
Find every scheduled job on a machine: user crontabs, system files, cron.d, periodic folders, anacron and systemd timers.
OpenBack up data with rsync
Build dated, space-saving snapshot backups with rsync and hard links, pull them from a second machine and test restores.
OpenAlmaLinux
AlmaLinux 9 for hosting panels and software written for the Red Hat family, with no subscription to buy.
OpenFAQ
Quick Answers
Run crontab -e as the user who should run it, add a line with five time fields and the command, then save.
Usually a missing PATH or relative paths. Cron has a minimal environment, so use full paths and log the output to see errors.
Redirect it to a log file or pipe it through systemd-cat and read it with journalctl.
Wrap the command in flock -n with a lock file so a second run exits while the first is busy.
crond, from the cronie package. On Debian and Ubuntu it is cron.