auth=`find /tmp -user $LOGNAME -type s -name "*agent*" -print >/dev/null`
SSH_AUTH_SOCK=$auth
export SSH_AUTH_SOCK
It looks like you're relying on a forwarded ssh-agent connection for the private key / passphrase. That appears to be what creates the dependancy on you keeping an interactive login session open or establishing one before the batch job runs.
How to avoid that dependancy?
Typically one sets up a dedicated ssh key-pair without a passphrase to be used by batch jobs.
You instruct the batch job to use that particular private key with an ssh -i /path/to/id_rsa.batch ...
Like always you copy the public key and add it to the remote ˜/.ssh/authorized_keys
but for security you make use of the often ignored options field to add restrictions to what is allowed by connections authenticating with that particular key pair.
Add the for example the restrictions: no-port-forwarding
no-X11-forwarding
, no-agent-forwarding
,no-pty
, from=IP-address
and/or others.
See authorized_keys file format description in https://www.freebsd.org/cgi/man.cgi?sshd(8) for their meaning
cat ~/.ssh/authorized_keys
from=10.9.8.7 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB... comments start here
no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDE... other comment