Interview Prep/Shell Scripting/Scenarios: text processing and scheduling decisions

A scheduled job needs a database password. Where do you put it for cron and for systemd?

IntermediateScenarioScenarios: text processing and scheduling decisions
Answer

Situation read → Keep it out of the crontab line, the unit file (world-readable in /etc/systemd/system), the script and Git.

What I'd do →

  1. Cron: a file readable only by the job's user (install -m 0600 -o jobuser), read at runtime: DB_PASS=$(< /etc/jobs/db_pass), or a tool-specific file like ~/.pgpass.
  2. systemd 247+: credentials, which are exposed only to the service:
    [Service]
    LoadCredential=db_pass:/etc/jobs/db_pass
    ExecStart=/opt/jobs/report.sh
    
    The script reads "$CREDENTIALS_DIRECTORY/db_pass". LoadCredentialEncrypted= can keep it encrypted at rest with the TPM or host key.
  3. Cloud: fetch from AWS Secrets Manager or SSM Parameter Store with the instance role at runtime.
  4. Never use Environment=DB_PASS=... in a unit file; systemctl show reveals it.

Why → Each option limits who can read the secret and keeps it out of version control.

Interview tip → Mentioning LoadCredential= shows current systemd knowledge.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: text processing and scheduling decisions