Creating a script that is launched after suspend

In Zorin lite/XFCE it is so easy to set up special commands to autostart after suspend because you can choose the trigger at autostart applications and select suspend.

I assume that with gnome autostart, an entry added there is always executed automatically at login, not just after suspend.
I'd like to know how I can do this in Zorin core/Gnome to determine the trigger.
I tried several scripts but didn't get them to work.

Where should the script be saved? What file extension should the script have?

As a test, I wanted to have nautilus start automatically after suspend, for example.
I created a file named nautilus (also tried with nautilus.sh) and put it into
/usr/lib/systemd/system-sleep
and made it executable.
The content of the file

#!/bin/sh

case $1 in
  post)
    nautilus
  ;;
esac
#!/usr/bin/env bash

if [[ $1 == post ]]; then
    nautilus
fi

but they don't work.

Can someone please create a template for me that can be used to execute a specific terminal command after suspend, e.g. to run the command modprobe -r psmouse && modprobe psmouse?

Since this is not a session environment, Graphical objects will not launch from this directory.
The hooks are present, which is why on XFCE it works, since you can hook to it, rather than init from it. But Gnome restricts access to those hooks as it tries to be an integrated Desktop and restrict users from system level access like Microsoft.
So, you must sheepishly shuffle over to your User Account. Try ~/.config/systemd/user/ (create the directory if needed)

From path with .service file: ~/.config/systemd/user/psmouse.service

[Unit]
Description=Run custom command after suspend/resume
After=suspend.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/resume.sh

Referencing the resume script: /usr/local/bin/resume.sh

#!/usr/bin/env bash

modprobe -r psmouse
modprobe psmouse

Make sure files are executable and

systemctl --user daemon-reload

systemctl --user enable resume-script.service

For GUI applications like Nautilus, you may need to route through the user account:
Environment=DISPLAY=:0
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%U/bus

I am reading the documentation - I have not tested this fully.

1 Like

Thank you for your help, Aravisian. I'll try that later. I tried the scripts yesterday on my LM XFCE laptop (since I wrongly assumed a script in this systemd directory would be handled the same way in gnome and xfce), but they didn't work there either.

Suspend doesn't work at all on my virtual machine; the whole screen freezes as soon as I click the suspend button, and I have to shut down the VM via Virtualbox. That's a real shame, because unfortunately I can't test it that way, only on Zorin live stick. Do you know if this is always the case with virtual machines? I can only pause when I use the pause button in Virtualbox, but not in Zorin VM.

Do you know of a command that could be used in the script that doesn't open a graphical interface but can be perceived with the senses so that you can tell whether the script has worked successfully (as an alternative to nautilus)?

I didn't get it to work. The service was not found:

systemctl --user enable resume-script.service

I followed this guide in ubuntohandbook. There are some mistakes in the commands (e.g. 'system' needs to be replaced with 'systemctl').
And now I found a solution so that nautilus is started after suspend.

Unfortunately, you cannot become root by entering admin:// in the address field of the file manager after suspend. I made the setup only for the user because it didn't work systemwide and I wasn't sure where I should add the environment variable then.
But the script works.

The sleep delay can be added but is not needed.
Done—but pretty complicated to set up!

This method with the environment variable isn't secure, or is it? But without the environment variable the service failed with "cannot open display".
It's probably better to simply start the desired apps via the startup programs every time you log in and only use the method presented here for scripts or special terminal commands, e.g. to reload kernel modules.
Do you think I can remove the environment variable and replace "nautilus" with
"sudo modprobe -r psmouse &&
modprobe psmouse" without needing an extra script file?
Is there any way to see whether the command has been done after suspend?

Edit:
I tried with the command "sleep 5 && xrandr -s 800x600" which worked without setting a environment variable. So probably I can do it this way.

Oh, my mistake. I had shortened the example name and... forgot on one line

systemctl --user enable resume.service

I haven't tried it yet because I already had deleted all created files and USB live stick doesn't save anything on shutdown.
Should it not be here in this example
systemctl --user enable psmouse.service?

Is there no way to apply it systemwide?
I was unable to get it working using the instructions for system-wide installation, only the user specific ones.
It's not a big deal if it doesn't work, I'm just wondering what I made wrong.
There were errors shown like ...bad unit file setting...cannot open display...(the last one for nautilus, the first one for xrandr command)

So are all applications or commands that are added to autostart via GUI applied user-specific?

With systemctl --user enable resume.service it didn´t work.


I think I'll use the guide from ubuntuhandbook that worked. There was an extra [Install] section in the file which seems to be required.
But when I add

[Install]
WantedBy=suspend.target

or

[Install]
WantedBy=psmouse.service

this error message appears: