Ubuntu – Running script at shutdown and reboot to prevent Chromium session loss


I'm running Ubuntu 18.04.1 and I'm trying to create a script that shuts down Chromium properly before shutting down the system so that it doesn't give me "restore session" popup after booting. I figured I'll use killall to this and came up with

killall -HUP "chromium-browser --enable-pinch"

that seems to do the trick.

However, now I have a problem with actually running this script at shutdown or reboot.

First thing I tried was putting it in /etc/init.d as shutdownscript with chmod a+x and then symlinking it to rc0.d and rc6.d as K99shutdownscript and later K01ashutdownscript. However, that didn't work for me.

I thought maybe I should just create a new systemd service, so I created shutdownscriptnew.service in /etc/systemd/system with contents like this:

# Provides:          shutdownscript
# Required-Start:    
# Required-Stop:     
# Default-Start:     
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.

Description=Saves Chromium session
Before=shutdown.target reboot.target halt.target



Then I ran systemctl start shutdownscriptnew.service and systemctl enable shutdownscriptnew.service but it still didn't work as intended.
However, when I run systemctl stop shutdownscriptnew.service it actually does what it should do and closes chromium properly so that after I open it again it doesn't want to restore pages.

Ubuntu 18.04.1 LTS
GNOME Shell 3.28.2
Chromium installed from Ubuntu's software application (chromium-browser)
Shutting down from GNOME taskbar.

I also tried just installing Google Chrome from google.com/chrome/ but it still wanted to restore pages after reboot.

I specifically want to be able to run script like this and not fix chromium because I have similar issue with Spotify – it doesn't save my last song when I shut down my PC and I'm hoping to do something similar to it.

I guess the problem is with shutdownscriptnew.service but I searched and searched and can't find anything that works for me.

Thanks for help!

Best Answer

The issue is your script is running later than it should.

By the time the script runs, the display manager has already died and taken Chromium with it.

You need to have your script run at logout vice shutdown.

Edit: On GDM3, you can try /etc/gdm3/PostSession/Default

Related Question