GNOME session save and restore

This script is used to save and restore a desktop session.

Why this script?

Some desktop managers do offer some session management features.
Under GNOME it may be possible to run gnome-session-properties manually and turn on “Automatically remember running applications when logging out”.
However, some GNOME implementations (notably Ubuntu’s) appear to be deemphasizing or deprecating this feature in favour of the Suspend or Hibernate feature.

Both the automated session save and suspend feature however are limited to saving/restoring sessions only on clean logout/login.

I use this script because sometimes I just need to reboot, and sometimes I like to restore my session to a previous state.

How to use:

Download the script, place it on your PATH, and be sure to give it executable permissions.

To save the current session:
>session save

To restore the saved session:
>session restore

Dependencies:

This script uses Perl, xprop, wmctrl, and optionally xdotool.  To ensure that all dependencies exist use:
>yum install perl xorg-x11-utils wmctrl xdotool

This script is only tested on my Fedora / GNOME Shell desktop – no idea how it behaves on other systems.

Details:

The session restore command takes an optional additional “level” argument 0-2.  Two use-cases illustrate its use.

1. Restore window geometries:
- After rearranging the desktop layout with xrandr
- After restarting an application

I have an external monitor plugged into my laptop, and many windows open across multiple workspaces and screens.  At this point I save the session for future reference.  Then I unplug the external monitor to take the laptop on the road, losing window positions.  Later I return and plug the monitor back in.  I run the session restore to reset window geometries as they were.

>session restore 0

Restore existing windows: This is the default.  It scans through the saved window IDs and for each window ID found still running, resets its geometry to the saved values.

>session restore 1

Restore matching windows: This goes one step further.  In case an application was shut down and restarted since the session was saved, the window manager will often give it the same ID as before, but sometimes this is not possible.  In this case, the restore will find a window with similar properties and set its geometry instead.

2. Restore running applications:
- After system reboot:
  – Kernel upgrade
  – Hardware upgrade
  – Non-recoverable software error
  – Battery dead
  – Power outage
- After Application crash or accidentally closing an application

In this case, the session is shut down for any number of reasons.  Hopefully the session was saved at some point before this – if you always have the same applications running, then saving it once will do; if your session changes all the time, then perhaps add session saving to cron.

>session restore 2

Restore missing application: Applications for which no matching instance is found are restored using their original command-line.

Note: The session script never shuts down applications.
Note: The session is stored in ~/.config/gnome-session/session.ini

Limitations:

There are many currently, but it saves me about 80% of the work of restoring a session manually.

The script contains a list of exceptions that is currently manually maintained. It lists applications that should be handled specially when restoring them (level 2).

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 2 to restart the application, (2) follow the application’s session restore process, and (3) run session restore 1 to restore its geometries.

Some upgrades are planned.

17 comments to GNOME session save and restore

  • Svenja

    Thanks a lot, just what I ve been looking for!

  • Didier

    Dealing with agonizing Gnome Shell crashes and restarts (may be due to buggy extensions), while having 30+ windows (browser windows, remote terminals, the lot) open, spread over 8 workspaces.

    This script is simply the most useful timesaver I’ve come across in ages.

    A sincere thank you.

  • Ethan

    This is exactly the kind of thing I was looking for! Why no distro has added this ability to facilitate a mental context switch, I do not know. I’m a grad student and I’ve got lots of different projects to work on (homework, research, personal projects, etc), when I switch between them I want to be able to just throw my session into some bar and switch to another for the task I’m switching to…with a large potential number of sessions to switch between at any given time.

    Any plans to try and add a GUI on top of this?

    Either way, great work!

  • Mauro

    Interestingly, it mostly works on Ubuntu (15 beta), Gnome 3. This is a very, very useful piece of software, in a number of ways.

    On Ubuntu, the geometry of some apps is not restored, or they fail to startup properly (Google Chrome, Brackets…), but more importantly,
    ~/.config/gnome-session/saved-session/session.ini

    Is cleared on every logout. I simply edited the script to save session.ini to my home folder.

  • Jori

    Wow, this is absolutely fantastic. Works great on Openbox. I just added it to my key bindings. Very neat. Thanks so much for posting this.

  • ivo

    Thanks a lot for this. I have a small suggestion: Put it on github!

  • Sander

    Hey all,

    I’m rather new to the ubuntu society, so I hope the question isn’t too much out of place here. I’m using Ubuntu 14.04 and since I work with a lot of different windows and workspaces, this script would be ideal for me to use. (the automated save does not work well at all actually).

    So I can across this page and did the following:

    1. I made sure all the software was properly installed.

    [sanderw@cygnusa] ~% sudo apt-get install perl wmctrl xdotool x11-utils
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    perl is already the newest version.
    x11-utils is already the newest version.
    wmctrl is already the newest version.
    xdotool is already the newest version.
    0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.

    2.
    I copied the script and put it in /home/sanderw/scripts. Changed the permissions to chmod +x and added the folder containing th script to my $PATH:

    [sanderw@cygnusa] ~% which session
    /home/sanderw/scripts/session

    3.
    Finally I try to execute the script and get the following error message:

    [sanderw@cygnusa] scripts% session save
    /home/sanderw/scripts/session: line 14: syntax error near unexpected token `(‘
    /home/sanderw/scripts/session: line 14: `my %exceptions = (‘

    I would really be very thankful if anybody could point out what is causing this problem! Thanks!

    - Sander

    • If I was to guess, it’s the Perl path (shebang) line at the top of the script:
      #!/bin/perl
      that probably has the wrong path for your system. Use:
      % which perl
      to find the correct path (assuming Perl is on the $PATH), and update it.
      Alternatively, you can run it through Perl manually this way:
      % /path/to/perl /path/to/session save

  • Sander

    Thanks for the quick reply! So you were correct about the cause of the error. I tried both your suggestions.
    The first one did not solve the problem:

    [sanderw@cygnusa] ~% which perl
    /usr/bin/perl
    [sanderw@cygnusa] ~% vim scripts/session

    Then changed the line:
    #!/bin/perl

    to:
    #!/usr/bin/perl

    and tried to run the script in the regular way:
    [sanderw@cygnusa] ~% session save
    /home/sanderw/scripts/session: line 14: syntax error near unexpected token `(‘
    /home/sanderw/scripts/session: line 14: `my %exceptions = (‘

    However, the alternative solution did the job:
    [sanderw@cygnusa] ~% perl scripts/session save
    Session saved.

    I still don’t quite understand why one works and the other doesn’t, but at least I’m able to use this very nice script!

    Thanks again!!

  • [...] This script works great for me on Xubuntu. It’s more or less desktop-agnostic and in theory should also support Unity. From the original blog post by Arnon Weinberg: [...]

  • Aurelijus

    Hi,

    I want to use this script in Ubuntu gnome 15.10 but I get an error: grep: /home/user/.dbus/session-bus/*: Permission denied
    Anyway around it?

  • [...] Weinberg created a script that allows saving and restoring a desktop session. The script doesn’t work exactly like the [...]

  • [...] Weinberg created a script that allows saving and restoring a desktop session. The script doesn’t work exactly like the [...]

  • [...] Weinberg created a script which allows saving and restoring a desktop computer session . The script turn up useful info [...]

  • [...] You can check out the script code HERE. Or, if you want to download Arnon’s script (without my Unity fixes), see his post HERE. [...]

  • [...] Weinberg created a script that allows saving and restoring a desktop session. The script doesn’t work exactly like the [...]

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=""> <strike> <strong>


8 × four =