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

A job in root's crontab works, but the same job moved to the `deploy` user's crontab mails `sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper`.

BeginnerTroubleshootingTroubleshooting: cron and systemd timers
Answer

Likely causes → The script calls sudo systemctl reload nginx, and deploy has no passwordless sudo rule for it. Interactively sudo prompted; under cron there is no terminal to prompt on.

Diagnose →

sudo -l -U deploy                        # what deploy may run, and whether NOPASSWD
sudo -u deploy sudo -n systemctl reload nginx; echo $?   # -n = fail instead of prompting

Fix → Grant exactly the command needed, without a password, and call sudo with -n so it fails fast:

# /etc/sudoers.d/deploy-nginx   (edit with: visudo -f /etc/sudoers.d/deploy-nginx)
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl reload nginx
sudo -n /usr/bin/systemctl reload nginx

Never use -S with a password stored in the script.

Prevent → Scope sudo rules to exact command lines, validate with visudo -c, and use sudo -n in all unattended scripts.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: cron and systemd timers