You are not logged in.

#1 2016-03-20 00:16:54

STREBLO
Member
Registered: 2015-02-15
Posts: 135

[SOLVED] Cant Send Email With systemd services using mail

After I run a scrub on my btrfs system I'm trying to send an email to myself containing a log with the results of the scrub.

#!/bin/sh
# Run scrub and email response
# Override btrfs-scrub@.service and execute this
# placed in /usr/local/bin/btr-scrub.sh

scrub_target=$1

/usr/bin/btrfs scrub status ${scrub_target} > "/var/log/btrfs-scrub.log" 2>&1
/usr/bin/btrfs device stats ${scrub_target} >> "/var/log/btrfs-scrub.log"
/usr/bin/echo "Scrub of ${scrub_target} complete" >> "/var/log/btrfs-scrub.log"
/usr/bin/mail *****@******.com < "/var/log/btrfs-scrub.log"

Right now I am using a drop-in snippet and ExecStartPost to run a script that emails me.

Original service:

cat /usr/lib/systemd/system/btrfs-scrub@.service 
[Unit]
Description=Btrfs scrub on %f

[Service]
Nice=19
IOSchedulingClass=idle
ExecStart=/usr/bin/btrfs scrub start -B %f

Snippet:

[Service]
ExecStartPost=/usr/local/bin/btr-scrub.sh %f

If I attempt to run the script by itself without systemd it works fine, but as soon as I attempt to run it was systemd it doesn't seem to send the email anymore and just completes the scrub as well as writing a log. I have tried adding the drop-in snippet to btrfs-scrub@.service and to btrfs-scrub@home.service and neither work. What am I doing wrong?

Last edited by STREBLO (2016-03-24 23:43:37)

Offline

#2 2016-03-21 20:22:33

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Cant Send Email With systemd services using mail

Offline

#3 2016-03-22 00:14:09

STREBLO
Member
Registered: 2015-02-15
Posts: 135

Re: [SOLVED] Cant Send Email With systemd services using mail

karol wrote:

OK, I gave that a try to no avail...

I made a new unit and script:

[Unit]
Description=status email for scrubbing %f

[Service]
Type=oneshot
ExecStart=/usr/local/bin/btrfs-scrub-mail.sh %f
User=root
Group=systemd-journal

I also attempted using ExecStart=bash -c "/usr/local/bin/btrfs-scrub-mail.sh %f", makes no difference

#!/bin/bash
# Run scrub and email response

mail btrfs-scrub.${1}@*****.com <<EOF
    Scrub of ${1} results
    $(btrfs scrub status ${1})
    $(btrfs device stats ${1})
EOF

And when I run my script in the terminal with it works fine and I get:

    Scrub of /home results
    scrub status for 82fd3efc-dbb3-42df-bb42-89c71675a5dc
	scrub started at Sat Mar 19 16:30:18 2016 and finished after 00:06:43
	total bytes scrubbed: 153.29GiB with 0 errors
    [/dev/sdb2].write_io_errs   0
[/dev/sdb2].read_io_errs    0
[/dev/sdb2].flush_io_errs   0
[/dev/sdb2].corruption_errs 0
[/dev/sdb2].generation_errs 0

The status output says:

systemctl status email-scrub-status@-                                                                                                                        
● email-scrub-status@-.service - status email for scrubbing /
   Loaded: loaded (/etc/systemd/system/email-scrub-status@.service; static; vendor preset: disabled)
   Active: inactive (dead)

Mar 21 17:07:29 Butters systemd[1]: Starting status email for scrubbing /...
Mar 21 17:07:29 Butters systemd[1]: Started status email for scrubbing /.

So for some reason it's working fine when I just run the script, but when I tried to run it as a service I get no email.

Last edited by STREBLO (2016-03-22 00:15:06)

Offline

#4 2016-03-22 06:56:09

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,433

Re: [SOLVED] Cant Send Email With systemd services using mail

you have created a oneshot service (that runs only one time), that plays *mail.sh script, but you have not defined when to start that service, something like "after=running scrub??" or every x hours/minutes, therefor it is inactive (dead).

Offline

#5 2016-03-23 00:04:03

STREBLO
Member
Registered: 2015-02-15
Posts: 135

Re: [SOLVED] Cant Send Email With systemd services using mail

Docbroke wrote:

you have created a oneshot service (that runs only one time), that plays *mail.sh script, but you have not defined when to start that service, something like "after=running scrub??" or every x hours/minutes, therefor it is inactive (dead).

Whoops, didn't mean to leave that there...

Offline

#6 2016-03-23 00:31:34

STREBLO
Member
Registered: 2015-02-15
Posts: 135

Re: [SOLVED] Cant Send Email With systemd services using mail

OK, still dont know whats wrong, set it to Type=simple and no difference... What am I missing?

Offline

#7 2016-03-23 04:18:06

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,433

Re: [SOLVED] Cant Send Email With systemd services using mail

Well let's rethink this.
Why you want to create a service? What you want is to send you a mail after you run scrub. You run scrub manually and your script sends mail after that. In my view you don't need a service, unless you want to automate  the running of scrub every x min. Let me give you an example

[Unit]
Description=Download Nifty Value

[Service]
Type=simple
ExecStart=/home/sharad/bin/nifty.sh
Restart=always
RestartSec=5

this servcie starts my script nifty.sh every 5 seconds. So if you want to run scrub every x second/min, you can use this type of service, but if you want to start it manually there is no need for service your script just does that.

Last edited by Docbroke (2016-03-23 04:19:20)

Offline

#8 2016-03-23 18:49:26

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Cant Send Email With systemd services using mail

https://bbs.archlinux.org/viewtopic.php … 5#p1608555 suggest using ssmpt instead of mail.

Offline

#9 2016-03-24 20:40:07

STREBLO
Member
Registered: 2015-02-15
Posts: 135

Re: [SOLVED] Cant Send Email With systemd services using mail

Docbroke wrote:

Well let's rethink this.
Why you want to create a service? What you want is to send you a mail after you run scrub. You run scrub manually and your script sends mail after that. In my view you don't need a service, unless you want to automate  the running of scrub every x min. Let me give you an example

[Unit]
Description=Download Nifty Value

[Service]
Type=simple
ExecStart=/home/sharad/bin/nifty.sh
Restart=always
RestartSec=5

this servcie starts my script nifty.sh every 5 seconds. So if you want to run scrub every x second/min, you can use this type of service, but if you want to start it manually there is no need for service your script just does that.

i'm using the service to test. What i want to do is have the scrub service, which is a different unit, send me an e-mail at after every scrub. So i want the scrub unit to send an email post scrub.

Last edited by STREBLO (2016-03-25 00:33:14)

Offline

#10 2016-03-24 23:42:34

STREBLO
Member
Registered: 2015-02-15
Posts: 135

Re: [SOLVED] Cant Send Email With systemd services using mail

karol wrote:

https://bbs.archlinux.org/viewtopic.php … 5#p1608555 suggest using ssmpt instead of mail.

MY HERO!

sSMTP works perfectly. I wonder why mail doesn't work with services... Anyway is sSMTP is working for me so that solves my problem. Thanks to everybody who helped.

Offline

#11 2016-03-25 00:11:01

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

Re: [SOLVED] Cant Send Email With systemd services using mail

Just in case you want to try one more thing besides sSMTP, try setting the MAIL environment variable inside your service file...that's typically what's needed for the mail command to work. You can see what it's set to in your regular shell environment with `env|grep MAIL`.

Scott

Offline

#12 2016-04-16 02:45:08

Celti
Member
From: Phoenix, AZ, USA
Registered: 2004-02-28
Posts: 43
Website

Re: [SOLVED] Cant Send Email With systemd services using mail

Ran into this problem myself today; the real solution is to put mail into "batch mode" with the "-#" switch.

The reason mail fails inside systemd without this switch is because by default, mail sends its messages asynchronously. In the default configuration, mail processes and starts sending the message, the mail process and then the script calling it exit, and systemd kills the entire control group — including the sendmail process mail spawned that hasn't finished yet.

-# enables the sendwait configuration option (among several other choices useful for automated sending of messages), which keeps mail from wrapping its thing up until after sendmail (or whatever message sending mechanism it's configured to use) returns.

Last edited by Celti (2016-04-16 02:48:47)


“A stupid person can make only certain, limited types of errors; the mistakes open to a clever fellow are far broader. But to the one who knows how smart he is compared to everyone else, the possibilities for true idiocy are boundless.”  —S.K.Z. Brust

Offline

Board footer

Powered by FluxBB