2

I have setup unattended-upgrades on servers running Raspbian (Raspbian GNU/Linux 9.4 (stretch)). Version of Unattended-upgrades: 0.93.1+nmu1

Updates work, but I am having problems with the email reporting. I want to use mailx for sending reports. If I run the update with command unattended-upgrade -v -d the report gets sent and it uses the email configuration I have in /root/.mailrc.

When unattended upgrades are run by the Systemd timer (apt-daily-upgrade.timer), however, it won’t use mailx.

If sendmail is present it is used to send the mail. In that case, the mail gets sent, but the sender is root@hostname and the messages get flagged as spam.

If there is no sendmail I see this error in journal of apt-daily-upgrade:

Cannot start "/usr/sbin/sendmail": executable not found (adjust *sendmail* variable)

I cannot understand why different mail programs gets used depending on how the task was started.

I have tried to edit unattended-upgrades Python program to force it use mailx:

if os.path.exists(SENDMAIL_BINARY):
        ret = _send_mail_using_sendmail(from_email, to_email, subject, body)
    elif os.path.exists(MAIL_BINARY):
        ret = _send_mail_using_mailx(from_email, to_email, subject, body

I changed the variable SENDMAIL_BINARY to point to a non-existing path so it would forced to use mailx. This too worked when invoked unattended-upgrades manually but failed when it was run by Systemd. (And the above error about trying to use sendmail still gets logged.)

How can I force unattended upgrades to use mailx even when is run automatically by systemd and what is causing the difference in Mail program that gets used?

EDIT:

System unit file that runs Unattended upgrades:

[Unit]
Description=Daily apt upgrade and clean activities
Documentation=man:apt(8)
ConditionACPower=true
After=apt-daily.service

[Service]
Type=oneshot
ExecStart=/usr/lib/apt/apt.systemd.daily install
KillMode=process
TimeoutStopSec=900
2

Your question is a variation of the FAQ Why do things work differently under systemd?.

One of the benefits of systemd is that it provides a consistent execution environment. To error on the side of security and simplicity, the environment variables set are minimal.

The related docs on the systemd execution environment detail what's set.

You mentioned that your configuration was in the root home directory. man mailx confirms It is looking in ~/.mailrc, as opposed to the fixed path /root/.mailrc.

The systemd docs clarify that the $HOME variable is only set when the User= directive is used. You did not share your systemd service file, but I presume since you are running the task as root you did not use the User= directive. So that could explain part of your issue.

It also appears that a path you want may not be set by your $PATH environment variable when run by systemd. You can confirm this by replacing the ExecStart= line in your service with:

 ExecStart=/bin/echo "My path is $PATH"

If the mailx path is not listed, you can explicitly set with an Environment= directive.

If this explicit tips don't solve your issue, be sure to check out the FAQ linked above for more possibilities.

  • I ended up solving my problem by running unattended-upgares from Crontab instead of Systemd as that worked right away without any issues. But I belive I tips of this answer explain my issues when using Systemd. If I have time I might try them in practice later. – Madoc Comadrin Apr 16 '18 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.