Yet another web developer blog

Using systemd as user service manager with systemctl and journalctl in userspace

Common systemd usage is to manage global linux services, that can be created/modified only with root/sudo credentials.

But systemd have the great user mode, that allow to create and control any services without sudo permissions, that runs as user owner, we simply need to add the `--user` argument.

For allow to start user services on system boot, you must one-time launch the command for needed user: sudo loginctl enable-linger LOGIN

General commands to list and control the systemd user services:

List of all available services for user: systemctl --user list-units --type=service -a - example of output:

UNIT                             LOAD   ACTIVE   SUB     DESCRIPTION                                   
admin-mongo.service              loaded active   running admin-mongo.service              
dirmngr.service                  loaded inactive dead    GnuPG network certificate management daemon   
gpg-agent.service                loaded inactive dead    GnuPG cryptographic agent and passphrase cache

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

3 loaded units listed.

Show the status of specific service: systemctl --user status admin-mongo.service - example of output:

● dev.brick.ru_admin-mongo.service
   Loaded: loaded (/home/alice/.config/systemd/user/admin-mongo.service; enabled; vendor 
   Active: active (running) since Fri 2021-03-12 16:17:04 MSK; 1s ago
 Main PID: 30089 (node)
   CGroup: /user.slice/user-1001.slice/user@1001.service/admin-mongo.service
           ├─30089 node /usr/bin/yarn start
           ├─30104 /bin/sh -c node app.js
           └─30105 /usr/bin/node app.js

Mar 12 16:17:04 brs.brick.ru systemd[1598]: Started admin-mongo.service.
Mar 12 16:17:04 brs.brick.ru yarn[30089]: yarn run v1.22.10

Controlling the specific systemd user service:

  • Start: systemctl --user start admin-mongo.service
  • Stop: systemctl --user stop admin-mongo.service
  • Restart: systemctl --user restart admin-mongo.service
  • Watch logs online: journalctl --user -u admin-mongo -n 10 -f (10 - number of last lines to display)

Creating new services:

For create the new services you need to create the file in user's homefolder at subpath .config/systemd/user/SERVICENAME.service with content like this:

[Unit]
AssertPathExists=/home/alice/tools/adminMongo

[Service]
WorkingDirectory=/home/alicetools/adminMongo
Environment=GHOST_NODE_VERSION_CHECK=false
ExecStart=/usr/bin/yarn start
Restart=always
PrivateTmp=true
NoNewPrivileges=true

[Install]
WantedBy=default.target

If you modify the file after, don't forget to reload the service information via command: systemctl --user daemon-reload

If you got the: Exec format error

Note, that user's service files must not contain the User=alice and Group=alice lines, like the system-wide services, otherwise you will got the error:

Failed to start admin-mongo.service: Unit admin-mongo.service is not loaded properly: Exec format error.
See user logs and 'systemctl --user status admin-mongo.service' for details.

Solution is to remove (or comment-out via #) thouse lines in .service file, and do the daemon-reload.

Tags