Sphere
Log in Join
☆ 4

cron job runs but produces nothing, PATH is probably it

A backup script that's been working for months stopped doing anything in April. Debian 12 box, script in /etc/cron.d/backup, runs at 03:15 as root. The cron mail says /bin/sh: 1: aws: not found but the mail is easy to miss. aws s3 sync works fine when I run the same script by hand. I assume cron has a shorter PATH. What's the cleanest fix that doesn't involve hardcoding /usr/local/bin/aws in twelve places?

⚑ Report

4 replies

☆ 0
S sticky ·2026-09-28#1

Set PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin at the top of the crontab file, above the job line. Cron gives you a minimal environment, so that's the standard fix. Also add MAILTO=you@example.com at the top so you actually see errors, and redirect output to a log file.

⚑ Report
☆ 0
F fern ·2026-09-30#2

Adding PATH worked. Turned out the script also sources a virtualenv with source, which under /bin/sh failed with source: not found after I fixed the aws issue. Changed it to . /opt/backup/venv/bin/activate and pointed the cron line at SHELL=/bin/bash. Two separate environment problems stacked on top of each other.

⚑ Report
☆ 0
S syn ·2026-10-13#3

That'll do it. If you want fewer surprises, make the cron entry a one-liner that calls the script with an absolute interpreter: /usr/bin/bash /opt/backup/backup.sh >> /var/log/backup.log 2>&1. Then the script itself can set whatever it needs and you're not relying on cron's shell at all.

⚑ Report
☆ 0
B bash ·2026-10-23#4

Also check the systemd timer route while you're in there. Debian 12 has systemd, and a timer with Persistent=true will catch up after a reboot, which cron won't do. It also gets a saner environment by default. Not worth rewriting a working backup at 3am, but next time you touch it, consider it.

⚑ Report
Log in to reply.