A cron job fails with `KeyError: 'HOME'` or cannot find `~/.aws/config`.
IntermediateTroubleshootingRuntime, file, subprocess & encoding errors
Answer
Likely causes → Cron and systemd run with a minimal environment: no HOME in some setups, no profile scripts, different user, no AWS_PROFILE.
Diagnose →
* * * * * env > /tmp/cron-env.txt # temporary crontab entry to inspect
Fix → Set needed variables explicitly in the crontab or systemd unit (Environment=HOME=/home/ops AWS_PROFILE=audit), or better use an instance role so no config file is needed.
Prevent → Use systemd units with explicit environment; avoid depending on login-shell state.
More on Runtime, file, subprocess & encoding errors
`KeyError: 'Tags'` while processing `describe_instances` output.Beginner`TypeError: can't compare offset-naive and offset-aware datetimes` in a snapshot-age check.Beginner`UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte` reading a log file.Intermediate`UnicodeEncodeError: 'ascii' codec can't encode character '✓'` when printing in a container.Intermediate`PermissionError: [Errno 13] Permission denied: '/var/log/app/report.json'`Intermediate`OSError: [Errno 24] Too many open files` in a long-running Python service.Intermediate