You are not logged in.

#1 2017-02-18 22:32:29

SgPepper
Member
Registered: 2014-05-16
Posts: 20

[Solved] How to write a correct systemd suspend service file

Hi !

I have an issue that i'm trying to fix with a systemd service. I would need help with writing a correct systemd service, but if you have a better solution, that's much apreciated

So the issue is : if I suspend ('systemctl suspend') my system while the backlight is at the lowest (/sys/class/backlight/intel_backlight/actual_brightness is 0), the brightness will be set at max on resume, which is annoying. My solution was to use a systemd service that would run a script (that would set the backlight at 1 is needed) before suspending.

I tried to adapt the suspend service template from the wiki, with the help of documentation from the red hat website, but unfortunately, I'm unable to come up with something correct

Here's the service I wrote:

[Unit]
Description=User suspend actions
Before=sleep.target

[Service]
User=%I
Type=simple
Environment=DISPLAY=:0
ExecStart=/home/quentin/Desktop/./script.sh
ExecStop=/usr/bin/sleep 1

[Install]
WantedBy=sleep.target

$ systemctl status suspend@user.service

● suspend@user.service - User suspend actions
   Loaded: loaded (/etc/systemd/system/suspend@.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Sat 2017-02-18 23:17:23 CET; 5s ago
  Process: 22179 ExecStop=/usr/bin/sleep 1 (code=exited, status=217/USER)
  Process: 22177 ExecStart=/home/quentin/Desktop/./test.sh (code=exited, status=217/USER)
 Main PID: 22177 (code=exited, status=217/USER)

Feb 18 23:17:23 9470m_arch systemd[1]: Started User suspend actions.
Feb 18 23:17:23 9470m_arch systemd[1]: suspend@user.service: Main process exited, code=exited, status=217/USER
Feb 18 23:17:23 9470m_arch systemd[1]: suspend@user.service: Control process exited, code=exited status=217
Feb 18 23:17:23 9470m_arch systemd[1]: suspend@user.service: Unit entered failed state.
Feb 18 23:17:23 9470m_arch systemd[1]: suspend@user.service: Failed with result 'exit-code'.

$ journalctl

Feb 18 23:34:26 9470m_arch sudo[24099]:  quentin : TTY=pts/2 ; PWD=/home/quentin/Desktop ; USER=root ; COMMAND=/usr/bin/systemctl start suspend@user
Feb 18 23:34:26 9470m_arch sudo[24099]: pam_unix(sudo:session): session opened for user root by (uid=0)
Feb 18 23:34:26 9470m_arch systemd[1]: Started User suspend actions.
Feb 18 23:34:26 9470m_arch sudo[24099]: pam_unix(sudo:session): session closed for user root
Feb 18 23:34:26 9470m_arch systemd[24102]: suspend@user.service: Failed at step USER spawning /home/quentin/Desktop/./script.sh: No such process
Feb 18 23:34:26 9470m_arch systemd[1]: suspend@user.service: Main process exited, code=exited, status=217/USER
Feb 18 23:34:26 9470m_arch systemd[24104]: suspend@user.service: Failed at step USER spawning /usr/bin/sleep: No such process
Feb 18 23:34:26 9470m_arch systemd[1]: suspend@user.service: Control process exited, code=exited status=217
Feb 18 23:34:26 9470m_arch systemd[1]: suspend@user.service: Unit entered failed state.
Feb 18 23:34:26 9470m_arch systemd[1]: suspend@user.service: Failed with result 'exit-code'.

The file is executable, and I tried both with  '/home/quentin/Desktop/./script.sh' and  '/home/quentin/Desktop/script.sh', which gave similat results. Also, there *is* a file /usr/bin/sleep.

And if it helps, script.sh :

#! /bin/bash
backlightValue=$(cat /sys/class/backlight/intel_backlight/actual_brightness)
if [ $backlightValue -eq 0 ]; then
	xbacklight =1
fi
exit 

Any help is greatly apreciated, thanks a lot !
SgPepper

EDIT : added journalctl

Last edited by SgPepper (2017-02-20 14:32:51)

Offline

#2 2017-02-19 10:41:23

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [Solved] How to write a correct systemd suspend service file

Enable the suspend@user and resume@user services to have them started at boot.

You need to use the real user name not the 'user' word here.

So the 'User=%I' will be set to the right user name.

And

ExecStart=/home/quentin/Desktop/./script.sh

why /./ ?

Offline

#3 2017-02-19 15:55:05

SgPepper
Member
Registered: 2014-05-16
Posts: 20

Re: [Solved] How to write a correct systemd suspend service file

You need to use the real user name not the 'user' word here.

Thanks, that's this right there.

why /./ ?

That's how you usually run scripts, as I was unsure, I tried both this and /script.sh


Thanks a lot for your help !

Offline

#4 2017-02-19 16:02:08

loqs
Member
Registered: 2014-03-06
Posts: 17,321

Re: [Solved] How to write a correct systemd suspend service file

SgPepper wrote:

why /./ ?

That's how you usually run scripts, as I was unsure, I tried both this and /script.sh

./whatever.sh

Is how you call whatever.sh in the current directory as it is not usually on the $PATH variable of paths to search for matching filenames to execute.
In systemd units the full path is given and there is no $PATH so /./ is redundant.

Offline

Board footer

Powered by FluxBB