I have a server where a lot of ssh connections (with a certain command) will be handled. Each connection consumes a bit of memory so I aim to close sessions (and thus its shell/command) as soon as the connection drops.

I already tried in my /etc/ssh/sshd_config

TCPKeepAlive no
ClientAliveInterval 0
ClientAliveCountMax 0


TCPKeepAlive yes
ClientAliveInterval 1
ClientAliveCountMax 1

But when I open now a session "ssh user@server sleep.py" and then close via CTRL+C on the client side the session is still active.

My question: Why is the session still active and doesn't receive the SIGHUP signal? Anything I can do to make it work, so the shell is closed as soon as the connection drops?

What current happens:

Client: ssh user@server sleep.py


1  root       0:00 /bin/sh -c /bin/bash /etc/app/run.sh
9  root       0:00   /usr/sbin/sshd -D
36 root       0:00      sshd: git [priv]
44 git        0:00         sshd: git@notty
45 git        0:00            sh -c sleep.py

Client: pressed CTRL+C (commands exits) Server:

1  root       0:00 /bin/sh -c /bin/bash /etc/app/run.sh
45 git        0:00   sh -c sleep.py
9  root       0:00   /usr/sbin/sshd -D
  • You say at one point that you're running "ssh user@server", and at another point that you're running "ssh user@server sleep.py". Could you edit your question to clarify exactly what ssh command you're invoking? Please include the relevant options from the client-side .ssh/config file and the server-side .ssh/authorized_keys file.
    – Kenster
    Commented Jan 30, 2018 at 18:07

1 Answer 1


Ah, but the sshd process is terminating. It's just leaving beyond the python process.

Your problem is caused by the fact that python does not respond to the HUP signal by default. I wrote:


sh -c ./test.py </dev/null


import time

I then ran ./test.sh &, and then sent a HUP signal to the test.sh process. The test.sh process terminated, but the test.py process remained.

Your python program can respond to the HUP signal, but you must program it to do so. Check out the signal module.

You must log in to answer this question.

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