#!/bin/sh agent_socket="${XDG_RUNTIME_DIR:?XDG_RUNTIME_DIR is not set}/ssh-agent.sock" SSH_AUTH_SOCK="$agent_socket" /usr/bin/ssh-add -l >/dev/null 2>&1 agent_status=$? case "$agent_status" in 0|1) # Agent exists; status 1 only means it currently has no keys. exit 0 ;; 2) # No reachable agent. Remove only its stale runtime socket. rm -f -- "$agent_socket" exec /usr/bin/ssh-agent \ -D \ -a "$agent_socket" \ -t 8h ;; *) exit "$agent_status" ;; esac