You are not logged in.

#1 2021-02-15 11:30:45

monotiz
Member
Registered: 2007-10-20
Posts: 140

[SOLVED] systemd/User - Notifying about failed services

EDIT - solved here: https://bbs.archlinux.org/viewtopic.php … 5#p1956395

Hi,
I'm looking for a solution for notify a systemd service failure.

I'm able to send email for systemd service (following this guide: https://wiki.archlinux.org/index.php/Sy … _services).

I can't make the same things for --user service, like timers.
I tried to replicate the service.d folder in ~/.config/systemd/user:

% cat ~/.config/systemd/user/service.d/toplevel-override.conf
[Unit]
OnFailure=user-failure-notification@%n

% cat ~/.config/systemd/user/user-failure-notification@.service

[Unit]
Description=Send a notification about a failed systemd unit
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/systemd-email my-email %i
User=root
Group=systemd-journal

Unfortunatly no email for the --user service. What's wrong wtih it?

Thank you

Last edited by monotiz (2021-02-16 08:02:34)

Offline

#2 2021-02-15 16:37:45

Head_on_a_Stick
Member
From: The Wirral
Registered: 2014-02-20
Posts: 9,003
Website

Re: [SOLVED] systemd/User - Notifying about failed services

monotiz wrote:

What's wrong wtih it?

That's a bit difficult to say without seeing the actual notification script. Did you check the journal?

Disclaimer: I've never actually tried this so I don't know if it's possible.


Jin, Jîyan, Azadî

Offline

#3 2021-02-15 16:52:50

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,599

Re: [SOLVED] systemd/User - Notifying about failed services

monotiz wrote:

Hi,
I'm looking for a solution for notify a systemd service failure.

Using an utterly simple function for this, I get a notification on tmux bar, but you can use it anyway you like.

sf() {
  systemctl --failed --no-legend | awk '{ printf "%s ",$2; }'
}

Offline

#4 2021-02-15 17:00:46

monotiz
Member
Registered: 2007-10-20
Posts: 140

Re: [SOLVED] systemd/User - Notifying about failed services

Head_on_a_Stick wrote:

That's a bit difficult to say without seeing the actual notification script. Did you check the journal?

Disclaimer: I've never actually tried this so I don't know if it's possible.

In the previous code, check for user-failure-notification@.service.
It's call a systemd-email script that send an email to my account. It's work for the system service.


qinohe wrote:

Using an utterly simple function for this, I get a notification on tmux bar, but you can use it anyway you like.

Ok. I'm looking into it.

Last edited by monotiz (2021-02-16 07:31:55)

Offline

#5 2021-02-15 17:38:35

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: [SOLVED] systemd/User - Notifying about failed services

I see two immediate issues without being able to look more into it:

1. Unless something has changed, you can't have a system service dependency in a user service file (After=network.target)
2. You have "User=root" which I don't think is possible in a user service file.

Offline

#6 2021-02-15 18:34:27

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,599

Re: [SOLVED] systemd/User - Notifying about failed services

firecat53 wrote:

I see two immediate issues without being able to look more into it:

1. Unless something has changed, you can't have a system service dependency in a user service file (After=network.target)
2. You have "User=root" which I don't think is possible in a user service file.

Not only that, the 'override.conf' says 'start service - user-failure-notification - but there's nothing in it to check against. That wont work OC, you need to run a comparrison at least or a tools(read script F.I.) that does that job.
Also, I do agree with firecat53, a user service don't have root as user. Also to check if a service has failed you don't need to be root even on a default hardened-kernel!
I don't say you need not tweak this services but I think you get closer with:

# sf.service
[Unit]
Description=systemctl failed
OnFailure=sf-noti.service

[Service]
ExecStart=~/bin/service-checker sf


# sf-noti.service
[Unit]
Description=Send notification

[Service]
Type=simple
ExecStart=/usr/local/bin/systemd-email my-email %i
ExecStop=systemctl --user start sf.service
StandardError=journal

Offline

#7 2021-02-16 08:01:38

monotiz
Member
Registered: 2007-10-20
Posts: 140

Re: [SOLVED] systemd/User - Notifying about failed services

Hi guys... thank you for your reply. I solved! Here the solution (maybe help some others).

For the system service, check the wiki page: https://wiki.archlinux.org/index.php/Sy … d_services

For the systemd/User service, I made a simple test service to generate an error:

$ cat ~/.config/systemd/user/test.service
[Unit]
Description=Test for sendmail

[Service]
Type=oneshot
ExecStart=make an error here!

[Install]
WantedBy=default.target

Next, I created a toplevel-override:

$ cat ~/.config/systemd/user/service.d/toplevel-override.conf

[Unit]
OnFailure=user-failure-notification@%n

and a service for notification:

$ cat ~/.config/systemd/user/user-failure-notification@.service

[Unit]
Description=Send a notification about a failed systemd unit
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/systemd-user-email my-email %i
StandardError=journal

Creat an empty drop-in like descript in the wiki page:

$ mkdir ~/.config/systemd/user/user-failure-notification@.service.d
$ touch ~/.config/systemd/user/user-failure-notification@.service.d/toplevel-override.conf

Last, the systemd-user-mail script:

$ cat /usr/local/bin/systemd-user-email
#!/bin/sh

/usr/bin/sendmail -t <<ERRMAIL
To: $1
From: systemd <root@$HOSTNAME>
Subject: $2
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=UTF-8

$(systemctl status --user --full "$2")
ERRMAIL

Offline

Board footer

Powered by FluxBB