Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: logout from interactive subshell



On Oct 12,  6:07am, Lloyd Zusman wrote:
}
} "Com MN PG P E B Consultant 3"
} <mn-pg-p-e-b-consultant-3.com@xxxxxxxxxxx> writes:
} 
} >    # Now I want to exit
} >    exit
} >    exit
} >    logout
} 
} Put this into your ~/.zlogin:
} 
}   [[ ${SHLVL:-0} == 1 ]] && {
}     export LOGINPID=$$
}   }
} 
} Then, after your "# Now I want to exit" comment (above), do this:
} 
}   kill -9 $LOGINPID

Yowtch.  This is the right idea, but "kill -9" is way overboard; that
kills the process instantly without letting it do any cleanup (like
saving history), and effectively orphans the sub-shells, which you are
then counting on to exit on their own.

Much better would be to use "kill -HUP" which tells the shell process
its terminal connection has been closed (even though it hasn't, really).
Zsh will then send HUP signals to all its children [*] and exit mostly
in the same manner as if you'd typed "logout".

[*] Unless you have "setopt no_hup" in which case you'll need to do
this a slightly different way.

So here's a variation of Lloyd's suggestion:

----
# This goes in ~/.zprofile for login shells only:
export LOGINPID=$$

# This goes in ~/.zshrc for all interactive shells:
if [[ ${LOGINPID:-$$} != $$ ]]
then function logout { kill -HUP $LOGINPID; exit }
fi
----

Now if you want to stop all the shells, you type "logout", and otherwise
you type "exit".

If you have "setopt no_hup" and only want to kill shells, leaving other
processes running in the background, modify the ~/.zshrc part:

----
# This goes in ~/.zshrc for all interactive shells:
trap logout USR1

if [[ ${LOGINPID:-$$} != $$ ]]
then
  function logout { kill -USR1 $PPID; exit }
fi
----

That should cause a chain of USR1 signals to climb up to the ultimate
login shell, with each shell exiting after passing along the signal.
You can pick another trappable signal if USR1 is not appropriate.



Messages sorted by: Reverse Date, Date, Thread, Author