You are not logged in.

#1 2016-02-28 08:08:45

DoubleX667
Member
Registered: 2011-09-08
Posts: 15

[SOLVED] Send mail via .service file fails

Hi all!

I have a backup script which sends me a status mail in the end via ssmtp.

The only mail related line in the script is:

cat $STATUS | mail -s "BackupStatus" foo@bar.com


If I execute the script from terminal via "sudo ./backup" everything works fine. The backup and the mail.
But if I execute it via systemd service file, the backup works as expected, but I don't get a mail.
There is also no error message in the logs.

My backup.service file:

Description=Backup

[Service]
ExecStart=/home/user/backup
Type=simple

[Install]
WantedBy=multi-user.target

Any idea what I did wrong?
Thanks a lot for your help

Last edited by DoubleX667 (2016-02-29 21:52:32)

Offline

#2 2016-02-28 14:56:48

Awebb
Member
Registered: 2010-05-06
Posts: 6,286

Re: [SOLVED] Send mail via .service file fails

Does backup have the x attribute? Is home mounted with noexec? Is mail configured for root? What does the status or the journal say?

Last edited by Awebb (2016-02-28 14:57:06)

Offline

#3 2016-02-28 15:05:49

ajbibb
Member
Registered: 2012-02-12
Posts: 142

Re: [SOLVED] Send mail via .service file fails

I had a similar problem a while back.  The problem was that the mail command being called from the service file (called via a systemd timer) did not have proper environment set up.  When you call it from a user or root account it does.  Even supplying the exact path to the mail command was not enough. 

My solution, which I am sure is not the correct one, was to call  the mail command with:

su -c YOUR MAIL COMMAND <<< PASSWORD

Probably an even worse mistake was to use the root user instead of setting up a specific backup user, but for my very specific use case this solution is adequate. 

There is also a section in the Arch systemd timer wiki about setting up mail accounts which may help.

Offline

#4 2016-02-29 20:55:36

DoubleX667
Member
Registered: 2011-09-08
Posts: 15

Re: [SOLVED] Send mail via .service file fails

Thank your for your help.

It sound realistic to me, that the problem is the "missing" environment.

I tried it with "su -c" command. But it is the same. If I execute the script from terminal everything works fine, from the service file I don't get a mail.

I tried to set "User=root" in the service file. But that also doesn't help.

Is there a possibility to "set an correct environment" from a service file?
I also tried it with the absolute path of the mail command...

Offline

#5 2016-02-29 21:51:01

DoubleX667
Member
Registered: 2011-09-08
Posts: 15

Re: [SOLVED] Send mail via .service file fails

Ha! I got it working now!

Using ssmtp instead of mail did the trick.
So the solution is:

STATUSMAIL=$(cat $STATUS)
echo "Subject: BackupStatus
$STATUSMAIL" | ssmtp foo@bar.com

Maybe not very elegant, but working  :-)

Offline

Board footer

Powered by FluxBB