I have a cronjob that runs a bash script every couple of hours. This bash script connects via ssh to a server using ssh keys. The private key I use for connecting to the server is protected by a passphrase, so, in order for cron to run this without prompting me for the passphrase, I added my private key to Keychain (following this: https://apple.stackexchange.com/a/250572/321620) so that it remembers my passphrase when running the cronjob.
This works fine, whenever I'm logged into my computer user. But whenever I log out of my user or lock my screen, the ssh connection in the cronjob fails to connect. In other words, the ssh command can't access the key stored in Keychain (at least that's what I think it's happening).
Is there any way for Keychain to work even when I log out of my user?
Update: I used the parameter -v in my ssh command to get the verbose output and I can see the following error:
debug1: read_passphrase: can't open /dev/tty: Device not configured
Which I guess makes sense, since it's not reading the key from Keychain. In other words, it's not even supposed to open a tty and ask for a passphrase, that's where Keychain comes into play. And again, this works just fine when I'm logged in.