Restarting gpg-agent

Michel Messerschmidt lists at michel-messerschmidt.de
Sun Mar 14 22:16:00 CET 2010


On Sun, Mar 14, 2010 at 12:24:14PM -0700, James Moe wrote:
> Hello,
>   opensuse v11.2, linux 2.6.31.12-0.1-desktop x86_64, gpg v2.0.12.
>   The docs at <http://www.gnupg.org/> cover starting gpg-agent pretty
> well. What is missing is how to re-start it.
>   If gpg-agent is terminated for some reason, or the system is booted,
> the file <.gpg-agent.info> is left behind. Because the file exists, when
> .bashrc is run it detects the file and does not start gpg-agent.
>   Is there some way to:
> 1. Detect if gpg-agent is running. If not, erase <.gpg-agent.info>, or
> 2. Erase <.gpg-agent.info> at boot time.


This works for me (in .bashrc):

export GNUPGHOME="${HOME}/.gnupg"
GPGAGENT=/usr/bin/gpg-agent
GA_INFO_FILE="${GNUPGHOME}/gpg-agent-info-$(hostname)"
# check that gpg-agent is executable and enabled in the gpg config
if grep -qs '^[[:space:]]*use-agent' "${GNUPGHOME}/gpg.conf" &&
   test -x ${GPGAGENT}; then
    # always re-read the gpg-agent info file to find the running instance
    if [ -r "${GA_INFO_FILE}" ]; then
        . "${GA_INFO_FILE}"
    fi
    # start gpg-agent if no running instance is found
    if test -z "${GPG_AGENT_INFO}" ||
       ! kill -0 `grep GPG_AGENT_INFO ${GA_INFO_FILE} | cut -d: -f 2 -` 2>/dev/null; then
        # enable ssh support by default if set in global Xsession options
        if grep -qs '^[[:space:]]*use-ssh-agent' /etc/X11/Xsession.options; then
            GA_SSH=--enable-ssh-support
        fi
        # execute gpg-agent and export environment variables
        eval $(${GPGAGENT} --daemon ${GA_SSH} --sh --write-env-file=${GA_INFO_FILE})
    fi
    export GPG_AGENT_INFO
    export SSH_AUTH_SOCK
    export SSH_AGENT_PID
fi


HTH,
Michel



More information about the Gnupg-users mailing list