More Bash answers

by Danny
Published 3 min read

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
_10
30 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


_10
crontab -e # edit your crontab
_10
crontab -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
_10
0 9 * * 1-5 /home/ada/bin/standup.sh # 09:00 Monday to Friday
_10
0 0 1 * * /home/ada/bin/invoice.sh # midnight on the 1st
_10
0 */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.


_10
SHELL=/bin/bash
_10
PATH=/usr/local/bin:/usr/bin:/bin
_10
_10
0 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
_10
set -euo pipefail
_10
cd "$(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:


_10
0 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:


_10
echo "$(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:


_10
env -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. Write date +\%F in the crontab line, or move the command into a script.
  • The script is not executable. Run chmod +x on it, or call it as /bin/bash /path/to/script.sh.
  • Typing crontab -r by mistake. It removes your whole crontab without asking, and the key sits next to -e. Keep a copy with crontab -l before 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.lock on 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?