You are not logged in.

#1 2022-10-13 07:43:16

VertigoGo
Member
Registered: 2022-10-12
Posts: 3

[SOLVED] need help writing/configuring service file

Hello Arch forum. I am having trouble correctly writing a systemd .service file. The service I am trying to start is an xidlehook daemon that keeps track of inactivity and puts my computer to sleep after a certain period of time. The problem seems to be a timing issue and/or a dependency issue because if I manually restart the service it works fine with no complaints. However, on every boot it fails with seemingly no useful information as to why.

Here is my current xidlehook.service file which is in ~/.config/systemd/user. I've only changed a few things from the given .service file on the xidlehook github(which is probably outdated). Adding the 'Before=' line, changing the Display from ':0' to ':1', changing some of the options to xidlehook, and finally changing the target to 'default.target' from 'multi-user.target'. But the behavior is slightly better than the original since that doesn't work (probably because the Display variable is wrong). At least with this I can manually restart the service and it works.

[Unit]
Description=Automatic Screen Locker
Before=default.target

[Service]
Type=simple
Environment=DISPLAY=:1
Environment=XIDLEHOOK_SOCK=%t/xidlehook.socket
ExecStart=/usr/bin/xidlehook --detect-sleep --not-when-audio --not-when-fullscreen --socket $XIDLEHOOK_SOCK --timer 600 'systemctl suspend' ''

[Install]
WantedBy=default.target

Here is the result of systemctl --user status xidlehook.service. As you can see it fails immediately with no good reason as to why, at least not that I can understand. Small censor for privacy.

× xidlehook.service - Automatic Screen Locker
     Loaded: loaded (/home/username/.config/systemd/user/xidlehook.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Wed 2022-10-12 23:37:43 CDT; 2h 8min ago
   Duration: 12ms
    Process: 1040 ExecStart=/usr/bin/xidlehook --detect-sleep --not-when-audio --not-when-fullscreen --socket $XIDLEHOOK_SOCK --timer 600 systemctl suspend  (code=exited, status=1/FAILURE)
   Main PID: 1040 (code=exited, status=1/FAILURE)
        CPU: 3ms

Oct 12 23:37:43 Desk systemd[1023]: Started Automatic Screen Locker.
Oct 12 23:37:43 Desk systemd[1023]: xidlehook.service: Main process exited, code=exited, status=1/FAILURE
Oct 12 23:37:43 Desk systemd[1023]: xidlehook.service: Failed with result 'exit-code'.

And here is the result of a manual restart of the xidlehook.service

● xidlehook.service - Automatic Screen Locker
     Loaded: loaded (/home/username/.config/systemd/user/xidlehook.service; enabled; preset: enabled)
     Active: active (running) since Thu 2022-10-13 02:18:51 CDT; 7s ago
   Main PID: 13766 (xidlehook)
      Tasks: 2 (limit: 19043)
     Memory: 1.1M
        CPU: 4ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/xidlehook.service
             └─13766 /usr/bin/xidlehook --detect-sleep --not-when-audio --not-when-fullscreen --socket /run/user/1000/xidlehook.socket --timer 600 "systemctl suspend"

Oct 13 02:18:51 Desk systemd[1023]: Started Automatic Screen Locker.

I have tried to read the relevant documentation, unfortunately it will be a while before I figure this out by myself. Any advice or help is appreciated, thanks in advance!

Last edited by VertigoGo (2022-10-15 23:26:48)

Offline

#2 2022-10-13 08:02:52

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: [SOLVED] need help writing/configuring service file

Before=default.target

You're trying to start this before the default (graphical) target, but xhidelock cannot run before an X11 server is up.
=> How do you log in and start the session?

DISPLAY=:1 is btw. most likely false and exporting a DISPLAY should™ not be necessary if this is started as a user service at the right stage (eg. when you're starting it after the fact you probably don't need that variable?) and will rather case harm.

Offline

#3 2022-10-13 20:27:44

VertigoGo
Member
Registered: 2022-10-12
Posts: 3

Re: [SOLVED] need help writing/configuring service file

Thanks for the reply. You are correct the Before should be After, I was trying both to see if I might be reading and understanding the documentation wrong, it is now changed.

I have tried 2 ways of logging in, until recently i was logging in from the console, no graphics. And now I am using gdm to see if that might help. With the console the result of "echo $DISPLAY" is ':0' and with gdm the result is ':1'. I'm not sure if this is necessary because I don't know how service files work if the environment variable DISPLAY can be taken from the system or if it needs to be set here and match with the system's value.
I'm assuming that either way of logging runs my xinitrc file which is here

#!/bin/sh

userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap

# merge in defaults and keymaps

if [ -f $sysresources ]; then
    xrdb -merge $sysresources
fi

if [ -f $sysmodmap ]; then
    xmodmap $sysmodmap
fi

if [ -f "$userresources" ]; then
    xrdb -merge "$userresources"
fi

if [ -f "$usermodmap" ]; then
    xmodmap "$usermodmap"
fi

# start some nice programs

if [ -d /etc/X11/xinit/xinitrc.d ] ; then
    for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
        [ -x "$f" ] && . "$f"
    done
    unset f
fi

xrandr --auto
exec qtile start

I'm no expert but this might be relevant in my qtile config. This is python code.

@hook.subscribe.startup
def dbus_resgister():
    id = os.environ.get('DESKTOP_AUTOSTART_ID')
    if not id:
        return
    subprocess.Popen(['dbus-send',
                      '--session',
                      '--print-reply',
                      '--dest=org.gnome.SessionManager',
                      '/org/gnome/SessionManager',
                      'org.gnome.SessionManager.RegisterClient',
                      'string:qtile',
                      'string:' + id])

Offline

#4 2022-10-13 20:48:44

seth
Member
Registered: 2012-09-03
Posts: 49,951

Re: [SOLVED] need help writing/configuring service file

With the console the result of "echo $DISPLAY" is ':0' and with gdm the result is ':1'

That's correct, GDM will start another X11 server (if it runs on X11 itself)

I'm assuming that either way of logging runs my xinitrc file which is here

xinitrc is sourced by xinit, startx and XDM - not by GDM nor when you just login at the console.
GDM will likely source ~/.xprofile

If you start the session using anything that invokes xinitrc, running xidlehook is the easiest way.
Otherwise see https://bbs.archlinux.org/viewtopic.php … 6#p2036886
You'll need a target that gets triggered after the X11 server started (GDM likely logs you in before it starts the extra X11 server)

Also see https://wiki.archlinux.org/title/Autost … rg_startup

Offline

#5 2022-10-15 23:18:26

VertigoGo
Member
Registered: 2022-10-12
Posts: 3

Re: [SOLVED] need help writing/configuring service file

Ok I've had a bit of time to read your provided links and I think I solved it.
To start I created xsession.target in ~/.config/systemd/user/ it is identical to your link, but here it is for ease of access

[Unit]
Description=X session managed by systemd
BindsTo=graphical-session.target

I created the directory xsession.target.wants in the same path as xsession.target (might not be necessary). An important user error that I made is I assumed if you restart a service it will automatically correct the symlinks but that is not true. You have to explicitly disable the service then re-enable the service in order to get proper symlinks.
Here is the final xidlehook.session, as was said by Seth, DISPLAY environment variable does not need to be redefined here. Probably because https://wiki.archlinux.org/title/System … Y,-DISPLAY

[Unit]
Description=Automatic Screen Locker
PartOf=graphical-session.target
After=graphical-session.target

[Service]
Type=simple
Environment=XIDLEHOOK_SOCK=%t/xidlehook.socket
ExecStart=/usr/bin/xidlehook --detect-sleep --not-when-audio --not-when-fullscreen --socket $XIDLEHOOK_SOCK --timer 600 'systemctl suspend' ''

[Install]
WantedBy=xsession.target

And finally for gdm I created the file ~/.xprofile

systemctl --no-block --user start xsession.target

It now auto-starts properly and I can manage it with systemd and all the benefits that come from that.

Offline

Board footer

Powered by FluxBB