Permission denied (publickey) error on Linux when using rsync in script

I have rsync command in my scripts:

rsync -av --delete --numeric-ids --relative --delete-excluded --exclude-from=/etc/exclude.txt --rsh=/usr/bin/ssh root@ec2-box2:/home/ /nfs/ec2-box/

But it says: Permission denied (publickey). Here is error log:

[2020-07-29T22:21:02] Permission denied (publickey).
[2020-07-29T22:21:02] rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
[2020-07-29T22:21:02] rsync error: error in rsync protocol data stream (code 12) at io.c(226) [Receiver=3.1.3]

Error only happens when the command run using shell script or cron job. At the shell prompt it works correctly. How to fix it? Please give suggestions.

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki