Skip to main content
Topic: Mate terminal randomly doesn't save history on some tabs (Read 702 times) previous topic - next topic
0 Members and 4 Guests are viewing this topic.

Mate terminal randomly doesn't save history on some tabs

Recently I noticed history was not always saved by Mate terminal, I checked this by opening 6 tabs and running ls 1, ls 2 etc in each, then closing them all one by one, then opening a new terminal window and checking. I also copied .bash_history and deleted the contents in case there was too much in there, but no. The commands from some tabs are not saved. It worked normally in Terminator.

Code: [Select]
nano .bash_history
ls 6
ls 4
ls 3
ls 2
ls 1
nano .bash_history
ls 6
ls 5
ls 4
ls 3
ls 2
ls 1

Re: Mate terminal randomly doesn't save history on some tabs

Reply #1
Recently I noticed history was not always saved ,,,
The commands from some tabs are not saved.

I would add the following line to ~/.bashrc:

Code: [Select]
PROMPT_COMMAND="history -a"

If you are working with several terminals at the same time and want to have a single history, the following line at the end of ~/.bashrc will help:

Code: [Select]
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

To ensure that nothing disappears from the history, the limit of (500) must either be increased or cancelled completely.

RTFM:
https://www.baeldung.com/linux/bash-histsize-vs-histfilesize

Code: [Select]
#
# clean bash history with no limit
#

HISTCONTROL="ignorespace"
HISTIGNORE="checkupdates*:history*:sudo*"
HISTTIMEFORMAT='%d/%m/%y %T '
HISTFILESIZE=
HISTSIZE=
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
"Wer alles kann, macht nichts richtig"

Artix USE="runit openrc slim openbox lxde gtk2 qt4 qt5 qt6 conky
-gtk3 -gtk4 -adwaita{cursors,themes,icons} -gnome3 -kde -plasma -wayland "

Re: Mate terminal randomly doesn't save history on some tabs

Reply #2
I had this in my .bashrc among other things, and it has been working fine for many years without this kind of problem:
Code: [Select]
shopt -s histappend

If I comment that out then add just this part from your suggestions:
Code: [Select]
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

it works normally. They should do the same thing, but from what I gather from the manual descriptions, the PROMPT_COMMAND variable is run every time the terminal prompt is drawn.  So I guess the "history" command is flushing the buffers or something while the mate terminal is currently failing to do so.

Setting HISTFILESIZE and HISTSIZE to unlimited might be convenient but introduces a security risk for memory and drive space flooding attacks, I had set a very large number for HISTFILESIZE instead and not set HISTSIZE, it seems to default to 500 lines currently.

Mate desktop was upgraded last Spring to 1.27 then downgraded again to 1.26 this Autumn - but mate-terminal remains on 1.27 and is an older build (Aug 31st 2023) than the 1.26 components, perhaps there could be some kind of compatibility issue.

Re: Mate terminal randomly doesn't save history on some tabs

Reply #3
I've got this in my bashrc, because I routinely CTRL-R for oneliners I've used in the past. Comments are self-explanatory:
Code: [Select]
# Erase history dupes
export HISTCONTROL=erasedups
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# https://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
#export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Re: Mate terminal randomly doesn't save history on some tabs

Reply #4
Setting HISTFILESIZE and HISTSIZE to unlimited might be convenient but introduces a security risk for memory and drive space flooding attacks, ...

After Shellshock, I have neither heard nor read of any successful attacks on the memory and/or data storage via the bash.
So I can't really share your concerns.

Apart from that, the thread title is [imho] misleading, because neither desktops nor terminal emulators have any influence on bash functionality.
"Wer alles kann, macht nichts richtig"

Artix USE="runit openrc slim openbox lxde gtk2 qt4 qt5 qt6 conky
-gtk3 -gtk4 -adwaita{cursors,themes,icons} -gnome3 -kde -plasma -wayland "

Re: Mate terminal randomly doesn't save history on some tabs

Reply #5
The normal behaviour is for history to be saved when the tab is closed, and that is only happening randomly - but apparently only in the Mate terminal, not others. Issuing the command "history -a" silently behind the scenes every time the cursor prompt appears is a neat workaround because "history -a" causes the HISTSIZE buffer to be immediately written to .bash_history (or wherever you put it if defined to be elsewhere) but it's very inefficient in comparison, it also makes the HISTSIZE buffer redundant because only one line is ever saved, which indicates this is not the intended method. Limitless logs are a well known security vulnerability, and although it's unlikely to be a real world problem in Linux, there are many cases of bugs that have resulted in "no space on disk" lockups which are annoying, so it is worthwhile mentioning the possibility for anyone unaware of the risks, it's the same reason coredumps are disabled by default. If you have a 100 million (or some other random large number) line limit on .bash_history this can never happen, and it's still effectively limitless for most people.

Re: Mate terminal randomly doesn't save history on some tabs

Reply #6
The normal behaviour ...
... for most people.

"The tribal wisdom of the Dakota Indians, passed on from generation to generation, says that when you discover that you are riding a dead horse, the best strategy is to dismount."
"Wer alles kann, macht nichts richtig"

Artix USE="runit openrc slim openbox lxde gtk2 qt4 qt5 qt6 conky
-gtk3 -gtk4 -adwaita{cursors,themes,icons} -gnome3 -kde -plasma -wayland "

Re: Mate terminal randomly doesn't save history on some tabs

Reply #7
I don't know if it's related but I have similar problems with bash after today's updates...

What was updated.

Now I get this when I start my XFCE terminal:

Quote
bash: PROMPT_COMMAND: line 0: syntax error near unexpected token `;'
bash: PROMPT_COMMAND: line 0: `history -a; history -n; ; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'

Also, if I start midnight commander (mc) it starts after a long pause.

Am I the only one with this?

My .bashrc

And my .bash_profile has only one line:

Quote
[[ -f ~/.bashrc ]] && . ~/.bashrc

Should I downgrade something to check?


Re: Mate terminal randomly doesn't save history on some tabs

Reply #9
UPDATE: Downgraded lightdm to the previous version 1.32.0-5 and everything is fine now. No bash errors...

Re: Mate terminal randomly doesn't save history on some tabs

Reply #10
I came down to a difference between 1.32.0-5 and 1.32.0-6 versions in the /etc/lightdm/Xsession file

Instead of this in 1.32.0-5:

Code: [Select]
# Load profile
for file in "/etc/profile" "$HOME/.profile" "/etc/xprofile" "$HOME/.xprofile"; do
    if [ -f "$file" ]; then
        echo "Loading profile from $file";
        . "$file"
    fi
done

Now it's this in the new 1.32.0-6:

Code: [Select]
# From https://github.com/sddm/sddm/blob/develop/data/scripts/Xsession
# Note that the respective logout scripts are not sourced.
case $SHELL in
  */bash)
    [ -z "$BASH" ] && exec $SHELL --login $0 "$@"
    shopt -q login_shell || exec $SHELL --login $0 "$@"
    set +o posix
    ;;
  */zsh)
    [ -z "$ZSH_NAME" ] && exec $SHELL --login $0 "$@"
    [[ -o login ]] || exec $SHELL --login $0 "$@"
    emulate -R sh
    ;;
  */csh|*/tcsh)
    # [t]cshrc is always sourced automatically.
    # Note that sourcing csh.login after .cshrc is non-standard.
    xsess_tmp=`mktemp /tmp/xsess-env-XXXXXX`
    $SHELL -c "if (-f /etc/csh.login) source /etc/csh.login; if (-f ~/.login) source ~/.login; /bin/sh -c 'export -p' >! $xsess_tmp"
    . $xsess_tmp
    rm -f $xsess_tmp
    ;;
  */fish)
    [ -f /etc/profile ] && . /etc/profile
    [ -f $HOME/.profile ] && . $HOME/.profile
    xsess_tmp=`mktemp /tmp/xsess-env-XXXXXX`
    $SHELL --login -c "/bin/sh -c 'export -p' > $xsess_tmp"
    . $xsess_tmp
    rm -f $xsess_tmp
    ;;
  *) # Plain sh, ksh, and anything we do not know.
    [ -f /etc/profile ] && . /etc/profile
    [ -f "$HOME/.profile" ] && . "$HOME/.profile"
    ;;
esac

[ -f /etc/xprofile ] && . /etc/xprofile
[ -f /usr/local/etc/xprofile ] && . /usr/local/etc/xprofile
[ -f "$HOME/.xprofile" ] && . "$HOME/.xprofile"