0

I recently embedded an "alert" command into my PROMPT_COMMAND to give me a little notify-send screen notification with the exit code of each command (because I always forget to append the alert when a long-running job is started).

That works like a charm, but then I discovered a minor nuisance about it:

When I connect to another computer in my network via slogin and want to disconnect with exit, it states "logout" and then just sits there. When I press [ctrl]+[c] it is back to my local session without a problem. I figured out that if I chuck out the "auto-alert" in all SSH-sessions the problem disappears. It's not that big of a deal, but I just wonder what is happening and maybe how to fix it, if possible.

Here's the command (in my aliases):

alias alert='notify-send --urgency=low -i "$([ $? -eq 0 ] && echo terminal || echo error)" "$(history 1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

And this is in my .bashrc:

if [[ "$PROMPT_COMMAND" != alert* ]]; then # && ([ -z "$SSH_CLIENT" ] || [ -z "$SSH_TTY" ]); then
  PROMPT_COMMAND="alert; $PROMPT_COMMAND"
fi

If i add the comment to the line (remove ; then #), the slogin session exits immediately without a problem (but also doesn't notify me of long-running jobs ending, of course).

  • A marginal comment on your first code section: you could advantageously replace history | tail -n1 with history 1, I think. Also if you are testing arithmetic values in bash, go for [ $? -eq 0 ] or if you test on strings, go for: [ "$?" == "0" ] . In the latter form a single = sign is also accepted. Mixing things up is a good recipe for issues. – Cbhihe Dec 18 '18 at 17:09
  • @Cbhihe Thanks for the suggestions, I edited the script for these two minor "issues". Had not even thought about "history 1", very neat. Sadly, both changes had no effect on the logout procedure.. – BUFU Dec 19 '18 at 10:10

Your Answer

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

Browse other questions tagged or ask your own question.