History function in SSH

Hi
And as this is my first message many thanks for this nice software.
I’m using it on a brand new K1pro from Mecool.
One of the first features I enjoyed when I discovered Linux is the persistent history function on the console.
Here it doesn’t seems to work when I use SSH .
I tried deleting everything in /storage/.ash_ history and it seems OK now.
Beware that it needs at least to close the SSH session to have anything written in the file
edit: added a missing “.” TKS Habibie

It annoyed me too for a long time and I decided to make something. It saves history when terminal window is closed and on exit.
The magic code is in /storage/.profile file.

CoreELEC:~ # cat /storage/.profile
func_exit() {
  sync
}

trap func_exit EXIT SIGHUP

I also made another version which saves current folder to a file and changes folder when login back.

CoreELEC:~ # cat /storage/.profile
func_exit() {
  pwd >/storage/.storepwd
  sync
}

trap func_exit EXIT SIGHUP

case $- in
*i*)
  if [ -f /storage/.storepwd ]; then
    dir="$(cat /storage/.storepwd)"
    [ -d "$dir" ] && cd "$dir"
    rm /storage/.storepwd
  fi
  ;;
*)
  ;;
esac


CoreELEC:~ # cd downloads/

CoreELEC:~/downloads # exit

Connection to ********** closed.

me@somewhere:~/tmp/a$ ce
Warning: Permanently added '********' (ECDSA) to the list of known hosts.
##############################################
#                 CoreELEC                   #
#            https://coreelec.tv             #
##############################################

CoreELEC: 9.0 (arm)
CoreELEC:~/downloads #
6 Likes

The file name should be preceded by a “.” /storage/.ash_ history and not /storage/ash_ history.

FYI, the history function works out of the box on both my Mecool KM8P and Sunveil T95Z+ boxes.

It does. But the question is how? What happen if you close terminal window? In this case for me last history is not saved. It is saved only when issuing exit command to log out from the session.

You clearly claimed in your OP that the history function on the console doesn't seems to work when I use SSH. and deleting everything in /storage/ash_ history and it seems OK now. In my cases, the SSH history function works out of the box. I don’t have to delete everything in the /storage/.ash_ history file.

What you said above looks like lost of an unsaved data when a window session gets terminated without a proper exit. In this case, this is not a problem. In other words, it works as expected, AFAICT. To save CPU resources, we don’t want to have each key pressed saved to a file right the way when it gets pressed.

I don’t understand what you are saying but that’s ok :slight_smile:

I’m happy how it works for me with this change.

R U saying even after you terminate your SSH window session, the SSH function history retains the previous key strokes?

If you close the ssh session without executing “exit” for instance with Ctrl-D or by closing the terminal window or when you call poweroff, reboot the history is not saved. @vpeter solution fixes it.

Additionally to what has already been said, you are wrong about the CPU usage and every keystroke being saved to a file while typing.
@vpeter’s solution just adds a function to SIGHUP (unexpected session closing), which properly writes back the history instead of trashing it.

Take a look a C write function to storage code and you will find out how much additional CPU resources, i.e. open/close file, etc., it will take to save a data to a storage. If every word typed is saved after the ENTER key is pressed, this will add up the CPU resources usage than to once save all the words typed when an exit function is called. For instance, if 3x ls+ENTER are typed by the users, there will be 3x write to a file function calls. Now, if these 3x ls+ENTER are typed and temporarily put in a memory buffer, there won’t be any CPU resources wasted to save the data until the exit function is called. During the exit function, only 1x write to a file function gets called. This will save 2x open/close function calls. Of course, on a fast computer, we won’t feel much a difference.

From what I see history is saved to history file /storage/.ash_history only on terminal exit. It doesn’t even save every command into file after pressing enter.
What I did is set a trap when terminal is closed and exit is not called from command line. And in this case it also saves history. There is zero more cpu usage.

And it is work well. Thank you! I needed this long time ago…

1 Like

Take a look at the function again (and to @vpeter’s comment above).
The history isn’t saved on every keystroke or every enter, but only once on a SIGHUP event.
Therefore it’s the same CPU usage you get when you type exit.

1 Like

That’s exactly what it does to prove my point, i.e. not to waste CPU resources to save every word (+ENTER) when typed, but do save them at once when an exit function gets called.

And that’s exactly, what vpeter’s function does, but only on another exit event.

Many thanks for your replies.
Shame on me I deleted the .ash_history file but it was huge (around 14K ) may be this is the problem.
It was probably created by me installing options with the GUI.
Here is a sample .ash_history file of another device (Xtreamer mxV )running openelec

Blockquote

OpenELEC:~ # cat /storage/.ash_history
precmd() { if [ ! “${PWD##$HOME}” ]; then MC_PWD=“~”; else [ “${PWD##$HOME/}” = “$PWD” ] && MC_PWD=“$PWD” || MC_PWD=“~/${PWD##$HOME/}”; fi; echo “$USER@$(hostname -s):$MC_PWD”; pwd>&6; kill -STOP $$; }; PRECMD=precmd; PS1=‘$($PRECMD)$ ’
precmd() { if [ ! “${PWD##$HOME}” ]; then MC_PWD=“~”; else [ “${PWD##$HOME/}” = “$PWD” ] && MC_PWD=“$PWD” || MC_PWD=“~/${PWD##$HOME/}”; fi; echo “$USER@$(hostname -s):$MC_PWD”; pwd>&6; kill -STOP $$; }; PRECMD=precmd; PS1=’$($PRECMD)$ ’
cd “printf "%b" '\0057'
precmd() { if [ ! “${PWD##$HOME}” ]; then MC_PWD=“~”; else [ “${PWD##$HOME/}” = “$PWD” ] && MC_PWD=“$PWD” || MC_PWD=“~/${PWD##$HOME/}”; fi; echo “$USER@$(hostname -s):$MC_PWD”; pwd>&6; kill -STOP $$; }; PRECMD=precmd; PS1=‘$($PRECMD)$ ’
cd “printf "%b" '\0057'
precmd() { if [ ! “${PWD##$HOME}” ]; then MC_PWD=“~”; else [ “${PWD##$HOME/}” = “$PWD” ] && MC_PWD=“$PWD” || MC_PWD=“~/${PWD##$HOME/}”; fi; echo “$USER@$(hostname -s):$MC_PWD”; pwd>&6; kill -STOP $$; }; PRECMD=precmd; PS1=’$($PRECMD)$ ’
cd “printf "%b" '\0057'
cd “printf "%b" '\0057storage'
precmd() { if [ ! “${PWD##$HOME}” ]; then MC_PWD=“~”; else [ “${PWD##$HOME/}” = “$PWD” ] && MC_PWD=“$PWD” || MC_PWD=“~/${PWD##$HOME/}”; fi; echo “$USER@$(hostname -s):$MC_PWD”; pwd>&6; kill -STOP $$; }; PRECMD=precmd; PS1=‘$($PRECMD)$ ’
cd “printf "%b" '\0057'
cd “printf "%b" '\0057storage'
precmd() { if [ ! “${PWD##$HOME}” ]; then MC_PWD=“~”; else [ “${PWD##$HOME/}” = “$PWD” ] && MC_PWD=“$PWD” || MC_PWD=“~/${PWD##$HOME/}”; fi; echo “$USER@$(hostname -s):$MC_PWD”; pwd>&6; kill -STOP $$; }; PRECMD=precmd; PS1=’$($PRECMD)$ ’
cd “printf "%b" '\0057'
cd “printf "%b" '\0057storage'
OpenELEC:~ #

Blockquote

For me it is garbage but sure “history” doesn’t work on this device