Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bignall/5cf2c14518145ff8b9d001203a81191c to your computer and use it in GitHub Desktop.
Save bignall/5cf2c14518145ff8b9d001203a81191c to your computer and use it in GitHub Desktop.
Ensures that you don't lose your konsole sessions when you crash/ reboot Linux

Usage

  • Save the two files below. I put them in '~/.scripts'
  • Modify the configuration in konsole-watcher.sh to your liking
  • Modify the location of konsole-watcher.sh in konsole-watcher-autostart.sh if you chose to put it somewhere besides ~/.scripts
  • Create the directory ~/.konsole (or other location you chose for the save file)
  • Create a symlink to konsole-watcher-autostart.sh in '~/.config/autostart-scripts
  • or open System Settings > Startup and Shutdown and add a script to the Script Files (it does the same thing as creating the symlink)
  • You might want to add a menu entry as well in case KDE autostart doesn't work for some reason

Problem that it solves

  • Automatically saves your current Konsole sessions with their working directories every 15 seconds (or other default you set)
  • Automatically starts Konsole when you log in with tabs for each or the sessions you had open previously

Credits

Based on https://gist.github.com/bguiz/9262902

#!/bin/bash
${HOME}/.scripts/konsole-watcher.sh restore & > /dev/null
#!/bin/bash
# Watches the konsole qdbus messages and saves session state changes so they
# can be restored easily
#configuration
WATCH_INTERVAL_SECONDS=15
SAVEFILE_TERMINAL="${HOME}/.konsole/current-tabs"
#restore if asked to
if [ "$1" = "restore" ] ; then
echo "Restoring..."
konsole --tabs-from-file ${SAVEFILE_TERMINAL} &
fi
# function to get the current sessions and write them to the save file
function getSessions {
local SESSIONS=$(qdbus org.kde.konsole | grep /Sessions/)
if [[ ${SESSIONS} ]] ; then
echo "# Most recent session list " $(date) > ${SAVEFILE_TERMINAL}
for i in ${SESSIONS}; do
local PROFILE=$(qdbus org.kde.konsole $i title 0)
local PROCESSID=$(qdbus org.kde.konsole $i processId)
local CWD=$(pwdx ${PROCESSID} | sed -e "s/^[0-9]*: //")
echo "workdir: ${CWD};; profile: ${PROFILE}" >> ${SAVEFILE_TERMINAL}
done
fi
}
#Update the Konsole sessions every WATCH_INTERVAL_SECONDS seconds
while true; do sleep ${WATCH_INTERVAL_SECONDS}; getSessions; done &
@bignall
Copy link
Author

bignall commented Sep 14, 2016

Updated konsole-watcher.sh so it doesn't overwrite the file when there are no konsole sessions running and added a date to the file comment.

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