How can I start a systemd service immediately after my desktop has initialised?

I’m trying to get a service to start once plasmashell has loaded, but although I now have a service file that does what I want when run manually, I’m struggling to work out how to trigger it automatically.

My user service file so far is:

[Unit]
Description=Hopefully more reliable autostarts for the desktop
After=plasma-workspace.target

[Service]
Type=forking
ExecStart=/home/tony/.autostart/startup.systemd
EnvironmentFile=/home/tony/.config/systemd/user/.profile
RemainAfterExit=yes

I’m guessing I need a WantedBy line under [Install], but can’t work out what to put there. Targets such as default.target and multi-user.target, which looked like good options, only run at startup, whereas I need this to trigger every time I log back in.

Any suggestions?
Thanks.

[Edit]: cracked it. Many thanks to all for their pointers. I’ve posted everything needed as a solution, so it’s there for reference if anyone else needs to do this.

Create a systemd user service - I suggest reading up on the docs.

There is a couple of Tutorials topics - which showcases the use of user services.

Example

Sorry, I probably wasn’t quite clear enough. What I’ve created so far is a systemd user service (in ~/.config/systemd/user), where all the rest of my service and timer units reside.
My problem is how to trigger it every time I log in.

Try to add this in the end of your service:

[Install]
WantedBy=graphical-session.target

Would that not start it at SDDM before the userspace is defined (what if there are multiple users on the system)? Probably still works because of direct instead of relative links in the script, but I think op was on to something with plasma-workspace.target but I could obv be wrong.

Also, @beermad don’t forget to use the --user option when you enable/disable/start/stop the service.

What I mean is:

[Install]
WantedBy=plasma-workspace.target

Don’t worry, I was using --user .

Both graphical-session.target and plasma-workspace.target appear to become active at the time I log in. As graphical-session didn’t work, I’m going to try plasma-workspace (perhaps I don’t want it in an “After” line, something else to try).

[Edit]: apparently not. Though I think one of those WantedBy lines should be what I need. More digging needed to work out why it’s still not triggering automatically. I’ll get there eventually…

You have to have an [Install] section or you can not activate it.

The After is making sure that things gets started in the correct order.

Thanks. That seems to fit with needing “WantedBy”.

Use the tools available to you, systemctl --user status name_of_service.service and journalctl --user -u name_of_service.service --no-pager to help you figure out what is going wrong.

The manual pages for systemd unit files is also very useful. Use internet and a browser, using man systemd would drive you insane. xD
Good luck, I’m sure you’ll figure it out.

Too late…

Many thanks to all for the useful pointers. I finally got this working once I twigged that I needed to force-close the service at logout, otherwise it wouldn’t be seen to be needed at startup.

Working autostart.service file:

[Unit]
Description=Hopefully more reliable autostarts for the desktop

# Make sure it doesn't run until Plasma is loaded
After=plasma-workspace.target

[Service]

# This probably ought to be something else, but it's working so it's staying.
Type=forking

# Wrapper script round everything I need to start
ExecStart=/home/tony/.autostart/startup.systemd

# Personalised environment variables
EnvironmentFile=/home/tony/.config/systemd/user/.profile

# Needed otherwise the service will eventually time out and shut down everything that it started.
RemainAfterExit=yes

[Install]

# Make sure it's triggered by Plasma
WantedBy=plasma-workspace.target

And it’s important to have a script which runs at logout to run:
systemctl --user stop automation.service

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.