0

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

and

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

Server:

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 Jan 30 '18 at 18:07
0

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:

test.sh

#!/bin/sh
sh -c ./test.py </dev/null

test.py

#!/usr/bin/python
import time
time.sleep(20)

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.