Make service not start after system suspend

Is there a way to make a service NOT resume after resuming system from suspend? That is, either send the service(s) a stop command, either when system goes into suspend (to ram or disk) or soon after the system has resumed from suspend.

I guess you could create a systemd service for that.

:joy: :joy: :joy: :joy:
I’m sorry. I thought ionotify. But your solution is so much better.

a Service to manage a service!

How do you tell the service to do something on suspend or resume?

According to this thread it’s going to be something like this:

[Unit]
Description=<your description>
After=suspend.target

[Service]
User=root
Type=oneshot
ExecStart=<your script here>
TimeoutSec=0
StandardOutput=syslog

[Install]
WantedBy=suspend.target

Note the WantedBy=suspend.target in the [Install] section.

Maybe a script similar to
/usr/lib/systemd/system-sleep/tlp
and save it in the same folder would to the job? Is there a better suitable place to put the script?

I don’t know if there’s a better location for the file, but I’d say create it there, with the same permissions as the others, of course.

Script in that location works good. I made a script that just runs

echo running suspend: $1 run as $USER >> /var/log/suspend/suspend.log
The resulting log file ends the line with “as”. So I guess the $USER variable is unset when suspending. Meaning the script to run has to be executable by everyone.

I don’t like to do modifications to /usr/lib though. I consider that as an area the packet manager should be the only one to make changes. I’d like to have this new file somewhere else.

According to thia page:

As far as I know there is no place for custom Linux scripts. The directory that should be used for custom install is the /opt directory so it would be the safest option to place them there. But keep in mind that for a script to be able to be used without the full path you need it to be included in the PATH variable.

Yeah. But how would the system know it should run the script /opt/lib/systemd/system-sleep/stopservices when going to sleep? To do so, I’d probably have to put a pointer (script or symlink) in /usr/lib/systemd/system-sleep , which leads me back to modifying file in the area I consider should be for the packet manager only. I my mind, there should be a file in /etc that specifies these things, but I can’t find it.

As far as I can tell, that’ll create a systemd unit. Like a daemon. I think. And if it’s enabled with

sudo systemdtl enable --now [scriptname]

Although I’m not 100% sure and might have it wrong and/or might be missing something. If I am, someone is welcome to correct me.