Save shell history before WSL termination on restart#469
Save shell history before WSL termination on restart#469Copilot wants to merge 3 commits intodevelopmentfrom
Conversation
Co-authored-by: crramirez <8397379+crramirez@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This pull request attempts to prevent shell history loss when WSL is terminated by the pengwin-setup script during restart scenarios. The change adds a history -a command before wsl.exe --terminate is executed to flush the bash history to disk.
Changes:
- Added
history -acommand inbye_message()function before WSL termination in interactive mode
pengwin-setup
Outdated
| message --title "Setup is complete." --msgbox "This window will be closed to apply the changes in the environment.\nPlease start Pengwin again \n\nYou may rerun pengwin-setup any time by typing: $ pengwin-setup\n\nYou may open a browser link to get help any time by typing: $ pengwin-help" 14 80 | ||
|
|
||
| if [[ ${#CMD_MENU_OPTIONS[*]} == 0 ]]; then #No automated installation | ||
| history -a |
There was a problem hiding this comment.
When history -a is called from within the pengwin-setup script (a child bash process), it will only save the history of the script's own bash process, not the parent interactive shell's history that the user was working in before running pengwin-setup.
Since bash processes maintain separate in-memory history lists, calling history -a in a subprocess cannot affect the parent shell's history. When wsl.exe --terminate kills all processes, the parent shell's unsaved history will still be lost.
To actually save the user's shell history, you would need to execute history -a in the user's interactive shell before the script terminates WSL. This could be achieved by:
- Creating a wrapper function that users source into their shell
- Using a trap mechanism in the user's shell profile
- Documenting this limitation and advising users to periodically save their history with
PROMPT_COMMANDor manualhistory -acalls
If this change was tested and confirmed to work, please provide details on the test scenario, as the current implementation appears to have this limitation based on standard bash behavior.
pengwin-setup
Outdated
| message --title "Setup is complete." --msgbox "This window will be closed to apply the changes in the environment.\nPlease start Pengwin again \n\nYou may rerun pengwin-setup any time by typing: $ pengwin-setup\n\nYou may open a browser link to get help any time by typing: $ pengwin-help" 14 80 | ||
|
|
||
| if [[ ${#CMD_MENU_OPTIONS[*]} == 0 ]]; then #No automated installation | ||
| history -a |
There was a problem hiding this comment.
Consider adding error handling for the history -a command. While it rarely fails, if the history file is read-only or the filesystem is full, it could produce an error that would be displayed to the user just before WSL terminates. Consider using history -a 2>/dev/null or history -a || true to suppress any potential errors.
Additionally, add a comment explaining the purpose of this command to help future maintainers understand why history is being saved at this specific point.
There was a problem hiding this comment.
Applied in baffbc6 — added 2>/dev/null || true for error suppression and a comment explaining the purpose of the history -a call.
Co-authored-by: crramirez <8397379+crramirez@users.noreply.github.com>
history -abeforewsl.exe --terminateto flush history to disk2>/dev/null || true) and explanatory comment per review feedback💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.