Skip to content

Instantly share code, notes, and snippets.

@bguiz
Last active February 11, 2023 22:04
Show Gist options
  • Save bguiz/9262902 to your computer and use it in GitHub Desktop.
Save bguiz/9262902 to your computer and use it in GitHub Desktop.
Ensures that you don't lose your gnome-terminal windows when you crash/ reboot Linux

Usage

  • create and alias/ symlink for the file, I use 'watcher.sh' in my home directory
  • when you fire up a gnome-terminal
    • enter ~/watcher.sh and leave that tab running in the background
  • when you crash and reboot, and fire up a gnome-terminal
    • enter ~/watcher.sh restore to restore all your windows and tabs that you previously had open, and it will cotninue watching automatically after that is done

Problem that it solves

First asked on superuser: http://superuser.com/a/722615/18095

In Ubuntu, I use the default gnome-terminal to run my commands. I typically have a couple of windows open, each with several tabs. When my OS crashes∗, for whatever reason, and I reboot, I lose all these terminals.

Sublime Text, Firefox and Chrome have spoilt me, as all of them give me the option to restore all the windows and tabs to exactly what they were doing prior to the crash, and I would like to be able to do the same with my terminal windows and tabs.

How can this be accomplished?

Spelling out the specifics of what I would like:

  • Must have:
    • Upon reboot, when I open terminal for the first time, the windows and tabs that were there before get re-opened
      • Either with or without a prompt is fine
    • Each tab is cd-ed into the folder that it was in before
  • Nice to have:
    • The bash_history for that tab is still there
      • so when I press the "up" and "down" keys, I would get what I expect within that tab

∗ Ubuntu running on a laptop whose drivers are not the most friendly, happens more often than I would like (in case you are wondering)

#!/bin/bash
#Saves gnome-terminal state at a regular intervals so that it can be restored easily
#configuration
WATCH_INTERVAL_SECONDS=15
SAVEFILE_TERMINAL="${HOME}/gnome-terminal-tabs"
#restore if asked to
if [ "$1" = "restore" ] ; then
echo "Restoring..."
gnome-terminal --load-config=${SAVEFILE_TERMINAL}
fi
#do the stuff
echo "Watching..."
watch -n${WATCH_INTERVAL_SECONDS} "gnome-terminal --save-config=${SAVEFILE_TERMINAL}; cat ${SAVEFILE_TERMINAL}"
@bignall
Copy link

bignall commented Sep 8, 2016

Thanks for your great example. Check out my fork designed for Konsole.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment