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:


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.

  • uhhhh… I’m not sure I get it, but I know that keychains are user-spesific – Sam Feb 22 at 16:25
  • So if I'm logged out and a cron job is supposed to run an ssh which uses a private ssh key stored in Keychain, it won't be able to access it? – ARR Feb 22 at 16:36
  • Yup, that sounds right – Sam Feb 22 at 16:56
  • If you duplicate the data from /User/yourname/Library/Keychains it would probably work, or maybe make an alias of the folder and put it in the Libray of the other account so that they are the same – Sam Feb 22 at 16:59

You must log in to answer this question.

Browse other questions tagged .