Add a job with crontab -e, read the five time fields, and avoid the usual cron traps: a minimal PATH, a different shell, unescaped percent signs and lost output.
The short answer
#Run crontab -e, add a line with five time fields followed by the command, and save. Use absolute paths and send the output to a log file, because cron runs with a very small environment and throws output away by default.
_10# minute hour day-of-month month day-of-week command_1030 2 * * * /home/ada/bin/backup.sh >> /home/ada/logs/backup.log 2>&1
That runs backup.sh at 02:30 every day and appends both normal output and errors to the log.
Why it happens
#The cron daemon wakes up every minute, checks each user's crontab and runs any line whose time fields match. It does not start a login shell, so none of your shell setup happens. Your .bashrc, .zshrc and .profile are not read, so aliases, version managers such as nvm, and anything you added to PATH are missing.
On most Linux systems the job gets a short PATH such as /usr/bin:/bin, runs under /bin/sh rather than Bash, and starts in your home directory. Output is mailed to you if local mail is set up, and otherwise discarded. That is why a script that works perfectly in your terminal can fail silently from cron.
How to fix it
#Write the schedule
_10crontab -e # edit your crontab_10crontab -l # list it
The five fields are minute (0-59), hour (0-23), day of month (1-31), month (1-12) and day of week (0-7, where 0 and 7 are both Sunday). Each can be * for any value, a list, a range or a step.
_10*/15 * * * * /home/ada/bin/check.sh # every 15 minutes_100 9 * * 1-5 /home/ada/bin/standup.sh # 09:00 Monday to Friday_100 0 1 * * /home/ada/bin/invoice.sh # midnight on the 1st_100 */6 * * * /home/ada/bin/sync.sh # every 6 hours, on the hour_10@reboot /home/ada/bin/start-worker.sh # once at boot
Shortcuts such as @reboot, @daily and @hourly are supported by the common Linux cron implementations. Times use the system time zone, so check it with date on the server.
Use absolute paths and set PATH
Either call every program by its full path, or set PATH at the top of the crontab.
_10SHELL=/bin/bash_10PATH=/usr/local/bin:/usr/bin:/bin_10_100 3 * * * /home/ada/bin/report.sh >> /home/ada/logs/report.log 2>&1
Find the full path of a command with command -v node. Inside the script, do not rely on the current directory either. Change to the right place first:
_10#!/usr/bin/env bash_10set -euo pipefail_10cd "$(dirname "$0")"_10_10./generate-report
If the script needs environment variables, load them explicitly in the script, for example by sourcing a file you control, rather than expecting your shell profile to do it.
Log the output
Redirect standard output and standard error so you can see what happened:
_100 3 * * * /home/ada/bin/report.sh >> /home/ada/logs/report.log 2>&1
Adding a timestamp inside the script makes the log much easier to read:
_10echo "$(date '+%F %T') starting report"
To check whether cron ran the job at all, look at the system log. On Debian and Ubuntu that is usually grep CRON /var/log/syslog or journalctl -u cron. On Fedora and RHEL the service is called crond.
Test with the cron environment
Before waiting for the schedule, run the script with an empty environment similar to cron's:
_10env -i HOME="$HOME" PATH=/usr/bin:/bin /bin/sh -c '/home/ada/bin/report.sh'
If it fails here, it will fail in cron.
Common pitfalls
#- Unescaped percent signs. Cron treats
%in the command as a newline. Writedate +\%Fin the crontab line, or move the command into a script. - The script is not executable. Run
chmod +xon it, or call it as/bin/bash /path/to/script.sh. - Typing
crontab -rby mistake. It removes your whole crontab without asking, and the key sits next to-e. Keep a copy withcrontab -lbefore editing. - Both day fields set. If day of month and day of week are both restricted, the job runs when either matches, not both.
- Overlapping runs. A slow job can still be running when the next one starts. Wrap it with
flock -n /tmp/report.lockon Linux to skip a run while the previous one is active. - macOS. cron still works, but Apple recommends launchd, and cron jobs may need Full Disk Access to read protected folders.
- Missing final newline. Some cron versions ignore the last line of a crontab if it does not end with a newline.
Was this resource helpful?