0

Scenario :

After working on my Linux server [CentOS 7] ,I typed reboot -f in my terminal, is it possible to retrieve my bash history after rebooting it?

I understand that reboot -f will forcefully reboot the machine. This is similar to pressing the power button of the CPU. No shutdown takes place. The system will reset instantly.

As per man page :

When called with --force or when in runlevel 0 or 6, this tool invokes the reboot(2) system call itself (with REBOOTCOMMAND argument passed) and directly reboots the system. Otherwise this simply invokes the shutdown(8) tool with the appropriate arguments without passing REBOOTCOMMAND argument.

Now my question is: Is it possible retrieve bash history after reboot -f?

6

No, as far as I know not as bash maintains history in memory and only writes or appends new history items when the shell exits.

When you "press the power button" (or run shutdown -f) obviously the shell does not exit gracefully and you lose any new history from that session as that exists only in memory. Previous history items from other sessions of course still persist as a reboot does not remove the existing history file.

Many terminal emulators keep a scrollback buffer from which you may recover some commands and others can even be configured to log everything.

2

In your .bashrc or the like, add PROMPT_COMMAND='history -a'. This flushes the history to disk every time the prompt shows. This is also useful for synchronizing the history between multiple shells.

It won't help you with the data already lost, but it will help preserve your command history moving forward.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.