-1.1 C
New York
Sunday, January 5, 2025

launchd – Allow launchctl service that hundreds ssh keys on login


Coming from the Linux world, I’ve arrange my share of systemd daemons, however I’ve solely this morning been compelled to experiment with launchctl. My current replace to Sequoia has made it in order that I now need to manually run ssh-add after each boot. Having to do that from a terminal session breaks another workflows I’ve arrange, so I’ve the next saved as ~/.config/scripts/add-ssh-keys.sh:

#!/bin/bash

# Make sure the SSH agent is operating and add your key
if ! ssh-add -l > /dev/null 2>&1; then
  eval "$(ssh-agent -s)"
fi

# Add the non-public SSH key to the agent
ssh-add -Ok ~/.ssh/my_private_key

I can simply verify that the ssh-add command works as written above when entered manually within the terminal. A fast ls confirms that the file is executable as I would flagged it. Subsequent, I’ve the next saved as ~/Library/LaunchAgents/com.person.addsshkeys.plist:

<?xml model="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist model="1.0">
<dict>
  <key>Label</key>
  <string>com.person.addsshkeys</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/bash</string>
    <string>/Customers/myusername/.config/scripts/add-ssh-keys.sh</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
</dict>
</plist>

I then ran launchctl load -w ~/Library/LaunchAgents/com.person.addsshkeys.plist and restarted my laptop. I run ssh-add -l and get the ‘agent has no identities’ error. If I run launchctl listing | grep com.person.addsshkeys I see the service has a standing of 2. If I run log present --predicate 'eventMessage incorporates "addsshkeys"' --info, I see many messages alongside the next strains:

xpcproxy: Launch constraint set on 00000000-0000-0000-0000-000000000000 /Customers/myusername/Library/LaunchAgents/com.person.addsshkeys.plist
launchd: [gui/### [######]:] service inactive: com.person.addsshkeys

What am I lacking?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles