Home · Linux · Vim · Programming · Trees · Quests

If you have a process that should start once when you login and stop when you log out, you need a systemd user service.

These instructions work on a clean Endeavor OS install, which is a fairly thin distribution of Arch Linux. Some additional basic setup, such as enabling user services in the first place, may be required on a pure Arch installation.

For more information, see the Arch Wiki’s coverage of this topic: https://wiki.archlinux.org/index.php/Systemd/User

Create a systemd unit file

User services go in /home/USERNAME/.config/systemd/user/. If that folder doesn’t exist yet, create it. Endeavour OS puts some things in there by default, such as the update notification service.

I use user services to mount s3 buckets to local folders using (s3fs)[https://github.com/s3fs-fuse/s3fs-fuse]. I use this to access my large digital music collection and also to store backups of my home folder. Here’s my unit file for the backup mount at /home/bagaag/.config/systemd/user/s3-backup.service:

[Unit]
Description=S3 Backup Mount

[Service]
ExecStart=s3fs -f bagaags-backup-bucket /mnt/backup

[Install]
WantedBy=default.target

In the example above, Description is used in logs and systemctl --user output. ExecStart is the command to run on login. And WantedBy allows for dependencies between services. This service doesn’t depend on any others so the default.target is used.

Note that if your ExecStart command exits immediately when run, you’ll want to add Type=oneshot under the ExectStart line. I added the -f flag to the s3fs command in my service, which keeps the service running in the foreground and provides the benefit of any output it generates being captured by systemd.

For a reference of all the options you can put in unit files, see https://www.freedesktop.org/software/systemd/man/systemd.unit.html.

Enable the service

Once you’ve created the unit file, use systemctl --user enable s3-backup.service to enable the service and then reboot.

If you change the unit file, use systemctl --user reenable s3-backup.servce to update it.

Check service status

After rebooting, use systemctl --user status s3-backup.service to see if it worked.