Restarting gpg-agent

Peter Pentchev roam at
Mon Mar 15 11:58:27 CET 2010

On Sun, Mar 14, 2010 at 10:16:00PM +0100, Michel Messerschmidt wrote:
> On Sun, Mar 14, 2010 at 12:24:14PM -0700, James Moe wrote:
> > Hello,
> >   opensuse v11.2, linux x86_64, gpg v2.0.12.
> >   The docs at <> 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 <> 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 <>, or
> > 2. Erase <> at boot time.
> This works for me (in .bashrc):

A good idea, and well written :)  Just one minor thing...

>     # 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

In this way, you risk a false positive if gpg-agent has died (or not
been started at all, but a file has been left over)
and there is another process with the same process ID.  This *can*
happen, whether by random chance at system startup, or by random
chance on a long-running system with PID's wrapping around.
A slightly better (if somewhat more convoluted) way could be
something like:

if [ -n "${GPG_AGENT_INFO}" ] && [ -r "$GA_INFO_FILE" ]; then
	gpg_agent_pid=`grep GPG_AGENT_INFO "${GA_INFO_FILE}" | cut -d: -f 2 -`
if [ -n "$gpg_agent_pid" ] &&
   expr "x$gpg_agent_pid" : 'x[0-9]*$' > /dev/null; then
	if pgrep gpg-agent | fgrep -qw "$gpg_agent_pid" > /dev/null; then

if [ -n "$gpg_agent_running" ]; then



Please don't take this as criticism, just an idea :)  And, of course,
it assumes that the OS has pgrep(1).


Peter Pentchev	roam at    roam at    roam at
PGP key:
Key fingerprint	2EE7 A7A5 17FC 124C F115  C354 651E EFB0 2527 DF13
What would this sentence be like if pi were 3?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: </pipermail/attachments/20100315/efc8f4c0/attachment.pgp>

More information about the Gnupg-users mailing list