Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

If PuTTY window is closed without explicitly ending the session, the login shell and subshells remain in the process tree (note the script.sh was running at the time SSH connection was dropped):

user  6841 12164   0   Jan 16 ?           0:02 -bash
user  9082  6841   0   Jan 17 ?           0:00 /bin/bash /usr/local/bin/script.sh arg1 arg2

Only solution I can come up with is writing an extra function into the script, roughly like this:

function cleanUp() {
    local parentPid="$1"
    local KEEPALIVE=86400    # 24h
    local lockDir="/tmp/${parentPid}_lockdir"

    if mkdir $lockDir; then
        sleep $KEEPALIVE
        kill -SIGKILL -$parentPid    # only sigkill seems to finish the processes
        rm -rf "$lockDir"


... and execute from the main script: 'cleanUp $PPID &'

What would be the correct way to clean up?

share|improve this question
The correct way would be to find out why the cleanup is not done by default – in other words, why your shell ignores the SIGHUP sent by sshd... –  grawity Jan 22 at 10:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.