GNOME session save and restore

Save and restore your GNOME Shell desktop active running application windows and their positions across multiple workspaces using an automated command line script.

Synopsis:

To save your session, press Alt+F2 or on a terminal:

>session save

To restore your session, press Alt+F2 or on a terminal:

>session restore

To restore your session including running applications:

>session restore missing

Installation:

Dependencies:

This script uses Perl, xprop+xwininfo, wmctrl, and optionally xdotool. To ensure that all dependencies exist use:

>sudo yum install -y perl xorg-x11-utils wmctrl xdotool

Note: I only test on my CentOS desktops. Others have reported the script working on Ubuntu, Xubuntu, and other desktops.

Download the script
Place it on your $PATH, and be sure to give it executable permissions:

>wget https://github.com/arnon-weinberg/session/raw/master/session
>chmod +x session
>sudo mv session /usr/local/bin

Use cases:

  • Restore workspace after reboot from kernel upgrade, hardware upgrade, non-recoverable error, battery died, or power outage.
  • Desktop layout messed on laptop after unplugging from an external monitor.
  • Restarting an application with multiple windows across workspaces.
  • Reopen application that crashed or was closed accidentally.
  • Remember and automatically restart open running active apps.
  • Reset application window geometries and arrangements over multiple desktop workspaces using command line or script.

Details:

>session save: Save session in default location (see below)
>session restore existing: Restore geometries of existing windows
>session restore matching: Restore geometries of matching windows [default]
>session restore missing: Restore geometries of missing windows

Available options:
–session: Set session filename [default = ~/.config/gnome-session/session.ini]
–debug=[0-3]: Set debug level

Limitations:
Restoring applications (session restore missing) only runs applications; it cannot restore their previous state – that is up to each application to do.
The script contains a list of exceptions that is currently manually maintained. It lists applications that should be handled specially when restoring them. Because some applications handle their own session save and restore, but they don’t handle restoring geometries, the session restore process can be a 3-step process sometimes: (1) Run session restore missing to restart the application, (2) follow the application’s session restore process, and (3) run session restore to restore its geometries.

Note: The session script never shuts down applications, and simply ignores applications not in the saved session.

Changes:

Relative to the previous version of this script, this upgrade has a much improved window matching algorithm, application running process, updates for recent applications, and fixes for several bugs.
The command line arguments have changed.

This script is now on GitHub.

Note: When submitting bug reports via GitHub, please include full debug output using –debug=3.

4 comments to GNOME session save and restore

  • xq34

    From my tests, and from the dependencies of this script, can you confirm that it does not work with the wayland version of gnome?

  • Zar

    Hello
    tnx for such a useful little piece of code 🙂
    I think Ive found a bug
    Have a look at the following session.ini:

    ‘0x04001477’ => {
    ‘workspace’ => ‘2’,
    ‘comm’ => ‘eog’,
    ‘pid’ => ‘6776’,
    ‘geometry’ => ‘10,95,53,833,1119’,
    ‘command’ => ‘eog/home/user/Desktop/BtKTaUsUN1g.jpg’,
    ‘exec’ => [
    ‘eog’,
    ‘/home/user/Desktop/BtKTaUsUN1g.jpg’
    ],
    ‘state’ => {
    ‘_NET_WM_STATE_FOCUSED’ => 1
    },
    ‘Name’ => ‘Eog’,
    ‘class’ => ‘eog.Eog’,
    ‘id’ => ‘0x04001477’,
    ‘name’ => ‘fedora mtlW6U9e1yY.jpg’
    },
    ‘0x04000007’ => {
    ‘state’ => {},
    ‘Name’ => ‘Eog’,
    ‘id’ => ‘0x04000007’,
    ‘name’ => ‘fedora BtKTaUsUN1g.jpg’,
    ‘class’ => ‘eog.Eog’,
    ‘command’ => ‘eog/home/user/Desktop/BtKTaUsUN1g.jpg’,
    ‘exec’ => [
    ‘eog’,
    ‘/home/user/Desktop/BtKTaUsUN1g.jpg’
    ],
    ‘comm’ => ‘eog’,
    ‘workspace’ => ‘2’,
    ‘pid’ => ‘6776’,
    ‘geometry’ => ‘10,882,42,1035,1158’
    },

    ‘exec’ and ‘command’ of the first application are wrong (they are the same as in the second application)

    Cheers

    • Glad you find it useful.
      This behaviour is common for applications that manage their own windows internally, and is not unique to eog. Unfortunately, this also means that multiple windows of such applications cannot be saved and restored by this script.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>