I am using Fedora MATE 31. I want to configure a service to run after every user login.
I found some useful information here (among other sources).
Based on those I set up the following:
# create needed directory
mkdir -p $HOME/.local/share/systemd/user
# Use nano to write the service file shown below
$ nano $HOME/.local/share/systemd/user/test1.service
[Unit]
Description=Testing On Login service
[Service]
Type=oneshot
RemainAfterExit=true
StandardOutput=journal
ExecStart=/bin/sh -c "echo $(date) > /home/$USER/.date"
[Install]
WantedBy=default.target
$ systemctl --user enable test1.service
Created symlink /home/$USER/.config/systemd/user/default.target.wants/test1.service → /home/$USER//.local/share/systemd/user/test1.service.
$ systemctl --user status test1
$ systemctl --user start test1
$ systemctl --user daemon-reload
After having reloaded the systemctl daemon I can do a cat ~/.date and see that the service has run successfully.
I can then reboot the system and also see that the service has run successfully.
The problem is that if I then log out and log back in, the service doesn’t run again! How can I make the service run after a login no matter if it is the first login after booting or subsequent ones?
I tried changing default.target to multi-user.target or graphical.target but that didn’t help.
The systemd user instance is started after the first login of a user and killed after the last session of the user is closed. Source: ArchWiki (IDK. On second though, the instance and service is a different things.)
Hi,
if I remember correctly, the systemd package in Fedora is built with an option to not kill user processes on logout. You can change this by setting KillUserProcesses=yes in /etc/systemd/logind.conf. Caveat: I think that breaks terminal multiplexers like GNU Screen.
$ cat logind.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See logind.conf(5) for details.
[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
#InhibitDelayMaxSec=5
#HandlePowerKey=poweroff
#HandleSuspendKey=suspend
#HandleHibernateKey=hibernate
#HandleLidSwitch=suspend
#HandleLidSwitchExternalPower=suspend
#HandleLidSwitchDocked=ignore
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
#HoldoffTimeoutSec=30s
#IdleAction=ignore
#IdleActionSec=30min
#RuntimeDirectorySize=10%
#RemoveIPC=yes
#InhibitorsMax=8192
#SessionsMax=8192
user processes should not be killed by default.
I also tried
$ loginctl enable-linger $username
but that didn’t seem to help.
I m thinking that maybe default.target is an event before the user enters a lightdm, in this case, session after a successful login and something else is needed to make this happen with systemd.
Thanks for the suggestion, unfortunately it didn’t work. I don’t know if it is because of how lightdm is setup.
/etc/lighdm/lightdm.conf has two options, # session-setup-script = Script to run when starting a user session (runs as root)
and # session-cleanup-script = Script to run when quitting a user session (runs as root)
.
Maybe that’s how it’s supposed to be done in MATE DE?