Interview Prep/Shell Scripting/Troubleshooting: cron and systemd timers

This crontab line never produces a log file, and cron mails `/bin/sh: 1: Syntax error: end of file unexpected (expecting ")")`: `0 2 * * * /opt/backup.sh > /var/log/backup-$(date +%F).log 2>&1`

IntermediateTroubleshootingTroubleshooting: cron and systemd timers
Answer

Likely causes → In a crontab, an unescaped % ends the command; everything after it is sent to the command's stdin. The shell receives /opt/backup.sh > /var/log/backup-$(date + and fails to parse it.

Diagnose → The error appears only in cron, never in your terminal. grep -n '%' <(crontab -l) finds the culprit.

Fix → Escape every %, or move the logic into the script where % is normal:

0 2 * * * /opt/backup.sh > /var/log/backup-$(date +\%F).log 2>&1

Better: let the script choose its own log file (exec >>"/var/log/opskit/backup-$(date +%F).log" 2>&1) and keep the crontab line trivial.

Prevent → Keep crontab lines to "time + absolute path to a script". Anything clever belongs in the script, where it can be linted and tested.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: cron and systemd timers