A cron job runs a script that calls `rsync` to another host over SSH, but in cron it hangs forever. What's going on and how do you design it properly?
IntermediateScenarioScheduling, automation and scripting
Answer
Situation read → No SSH agent and no TTY in cron; SSH is waiting for a password or host key confirmation.
What I'd do →
- Use a dedicated key without a passphrase, restricted on the remote side (
command=,from=). - Pre-populate
known_hosts. - Make SSH fail fast:
rsync -a -e "ssh -i /home/sync/.ssh/id_ed25519 -o BatchMode=yes -o ConnectTimeout=10" /data/ sync@backup:/data/
- Wrap with
timeout 2hso it can't hang forever.
Why → BatchMode=yes turns a hidden prompt into an immediate error you can see in logs.
Interview tip → Mention timeout as a general safety net for every scheduled job.
More on Scheduling, automation and scripting
A database dump must run every night at 1:30 AM. How do you schedule it with cron so it is reliable?BeginnerConvert that nightly cron job to a systemd timer. Why would you?IntermediateA cron job runs every 5 minutes, but sometimes takes 8 minutes and two copies overlap, corrupting data. How do you prevent that?BeginnerYou have 300 servers running the same cron job at midnight, and they hammer a shared API. How do you fix it?IntermediateA job must run on the last day of every month. Cron has no "last day" field. What do you do?IntermediateYou inherited a server and need to know every scheduled job on it. Where do you look?Beginner