Autostarting startupapplications after wake ubuntu 18.04

Hey there here a quick guide for setting up hibernate and more important autostarting the programs you placed in the autostarting tool.

  1. Set up hibernate:

I used this guide
https://wiki.ubuntuusers.de/Ruhezustand/ (Note: this is in german, but there are plenty of guides for this)
After that with
sudo hibernate
my system correctly hibernates and wakes up.

  1. Add hibernate button to gnome

I used this extension:
https://github.com/arelange/gnome-shell-extension-hibernate-status
But other then in the readme.md stated, I need to place the file contents in /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
and install the gnome-extension from here https://extensions.gnome.org/extension/755/hibernate-status-button/

  1. Autostart Programs

Now it works, with a few missing pieces, all my programs that starts, when my user logs in (the program that are specified in the startupprograms tool) are not starting except for signal, soo lets starts them:

Place a script for starting all programs in /lib/systemd/system-sleep/. And make it executable.

sudo touch /lib/systemd/system-sleep/your_script.sh 
sudo chown +x /lib/systemd/system-sleep/your_script.sh 

in there, place a case structure like this

#!/bin/sh

case "$1" in
        pre) #place here stuff done before going to sleep/hibernate
                ;;
        post) #place here stuff done on wake
                /start/some/applications
                ;;
esac

I could just placed all my startup applications in here, but then I have to extends this file if I want more programs to startup, and I already have a list. It is in this directory /home/user/.config/autostart/*.desktop here the ubuntu startupapplication stores every entry.

therefore I want all the .desktop to execute, but this is not really intended, (https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal)
the intended way is not possible to use, because the directory is not the installed applications directory therefore my startup.desktops are not found with gtk-launch <app-name>

but the deprecated way is working, I placed the skript following script /home/user/file/path/to/your/start.desktop.sh on my pc and make it executable.

#!/bin/sh
$(grep '^Exec' $1 | tail -1 | sed 's/^Exec=//' | sed 's/%.//' \
| sed 's/^"//g' | sed 's/" *$//g') &

I really don’t understand what this is exactly is doing, but I have to say grep is reeally powerful ^^

and call this file for all .desktop Files in /home/user/.config/autostart

The /lib/systemd/system-sleep/your_script.sh is now

#!/bin/sh

case "$1" in
        pre) #place here stuff done before going to sleep
                ;;
        post) #place here stuff done on wake
                for filename in /home/user/.config/autostart/*.desktop; do
                        if [ "$filename" != "/home/user/.config/autostart/signal-desktop.desktop" ]; then #exclude signal, because signal manages to startup
                                /home/user/file/path/to/your/start.desktop.sh $filename
                        fi
                        sleep 1 # sometime the script gets hung up
                done
                ;;
esac


now all my startup applications resume after wake.
More elegantly would be if wake would also wake those programs, but yeah.
If someone has a solution for this, it would be very appreciate for you to share.

Credits : (I of course did not come up with all this stuff)
starting .desktop Files from terminal https://itectec.com/ubuntu/ubuntu-running-a-desktop-file-in-the-terminal/
starting on wake https://askubuntu.com/questions/92218/how-to-execute-a-command-after-resume-from-suspend (answered Jun 5 '20 at 8:32)