I like to sign my git commits with my PGP key, so I was quite alarmed when I went to git commit -S but instead of prompting for my PGP key passphrase, git just started hanging. I haven't made a change to my GPG setup in several months and have made many commits since then with no problem. Additionally, when I attempt to view my private keys with gpg -K, gpg hangs. However, when I run gpg -k to view my public keys, it returns the list like normal. Hopefully someone will have some idea of what is causing this problem and how to fix it.

  • 1
    are you doing this over ssh? if so, do you have gpg-agent or similar setup in the .bashrc (etc) of the remote system, and configured to prompt on the remote's X display or similar? i've had similar problems in the past (e.g. ssh-ing from a terminal on my mythtv box to my desktop machine to run something requiring gpg. also had similar issues with ssh-agent), and i brute-force "fixed" it with export GPG_TTY=$(tty) in the .bashrc, to make sure the prompt is always on the current tty. i can't stand GUI passwd prompts anyway.
    – cas
    Jul 28, 2017 at 6:07
  • 1
    No, I'm not doing it over SSH. What's weird is that I found that if I kill gpg-agent, gpg works again. I'm trying to figure out why and make a long term solution. Jul 28, 2017 at 16:10
  • I just noticed in the gpg-agent man page that setting GPG_TTY as above isn't something I hacked up to work, it's required. The man page says you should always set it in your .bashrc as I did, and also says It is important that this environment variable always reflects the output of the tty command. - that must have been where I got it from. One other thing you need to be careful of is the pinentry program used by gpg-agent. I have mine set (in ~/.gnupg/gpg-agent.conf) to /usr/bin/pinentry-curses
    – cas
    Jul 28, 2017 at 16:40

2 Answers 2


I came across this exact issue (OSX Sierra 10.12.6, gpg/GnuPG 2.2.5)

Commands that would hang:

gpg -K # --list-secret-keys
gpg -d # --decrypt
gpg --edit-key
gpgconf --kill gpg-agent

My solution was the same as mentioned by John above (ie. kill gpg-agent) as most other methods on how-can-i-restart-gpg-agent would also hang.

# Solution    
pkill -9 gpg-agent

Then for signing git commits I set the tty env as mentioned by cas above and also at gpg-failed-to-sign-commit-object.

export GPG_TTY=$(tty)
  • So do you have to re-kill gpg every time you reboot? That's what I've been having to do for a long time now. I hope we'll find a way to fix it for good. Mar 22, 2018 at 2:33
  • Just started having the issue myself. Killing the gpg-agent seems to be the only way to get it working again. May 29, 2018 at 4:48
  • 1
    Got this issue on Ubuntu 22.04, and it persists even after killing gpg-agent.
    – Fred Qian
    Dec 4, 2022 at 3:13
  • Found a working solution here: stackoverflow.com/a/53641081/1708426
    – Fred Qian
    Dec 4, 2022 at 5:43
  • When I kill gpg-agent in a Visual Studio Code devcontainer, it comes right back. Aug 16 at 20:18
$ ps aux | grep -E "gpg-agent"
alper  28970   0.0   92436   3284   15:31 0:00 /usr/bin/gpg-agent --supervised

Here output variable contains 28970.

from subprocess import Popen, PIPE
import signal

def kill_process_by_name(process_name):
    p1 = Popen(["ps", "auxww"], stdout=PIPE)
    p2 = Popen(["grep", "-E", process_name], stdin=p1.stdout, stdout=PIPE)
    p1.stdout.close()  # noqa
    p3 = Popen(["awk", "{print $2}"], stdin=p2.stdout, stdout=PIPE)
    output = p3.communicate()[0].decode("utf-8").strip()
    lines = output.splitlines()  # awk may return more than one pid number
    for pid in lines:
        if pid.isnumeric():
            os.kill(int(pid), signal.SIGKILL)
  • This isn't really a solution to the problem, just a workaround. I'd advise against auto-killing processes in a script like this. Mar 9, 2022 at 18:05
  • Why are you against auto-killing processes in a script like this? Wouldn't you kill processes in a bash-script using kill -9 $(ps auxww | grep -E "[p]rocess_name" | awk '{print $2}') > /dev/null 2>&1?
    – alper
    Mar 9, 2022 at 18:50
  • that would be just as bad. My point is that automatically killing a process to get it to work isn't a solution. It's just an ugly workaround, and I wouldn't feel comfortable using it. Mar 10, 2022 at 19:42
  • What would you fell comfortable to use? Don't you ever automatically kill a process and restart it in your on computer, or ever restart the operating system if somehing unexpected happens?
    – alper
    Mar 10, 2022 at 22:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .