You are not logged in.
Pages: 1
Hi,
I have been playing around, trying to replace some cron functionality with systemd timers. I have some trouble with a script sending out email via the `mail` command. I boiled it down to the following minimal example (which doesn't actually involve timers because the problem is already with the systemd service):
A script sending some mail:
/usr/local/bin/example_minimal.sh
#!/usr/bin/env bash
mail -s "Minimal example" "my@mail.address" <<< test
echo "done"
And a service file, supposed to call this script
/etc/systemd/system/example_minimal.service
[Unit]
Description=Send a mail
[Service]
ExecStart=/usr/local/bin/example_minimal.sh
Now, calling the script directly works as expected, however with systemctl I get
> systemctl start example_minimal
> systemctl status example_minimal
● example_minimal.service - Send a mail
Loaded: loaded (/etc/systemd/system/example_minimal.service; static; vendor preset: disabled)
Active: inactive (dead)
Jul 20 14:45:33 ip-172-30-0-159 systemd[1]: Starting Send a mail...
Jul 20 14:45:33 ip-172-30-0-159 example_minimal.sh[5637]: done
So it looks like everything is fine (echo "done" gets executed) but no mail is being sent...
I managed to work around this by using either Type=forking or RemainAfterExit=yes but I don't really understand why this is needed/working!? Is this caused by the behaviour of the mail program?
In particular, I don't want to RemainAfterExit because it should send the mail again upon systemctl start, and for Type=forking the man page says the process should fork() itself which I am not doing (but maybe mail is?) What's the proper way to handle this?
Thanks
Patrick
Last edited by pesser (2015-07-21 09:31:41)
Offline
I managed to work around this by using either Type=forking or RemainAfterExit=yes but I don't really understand why this is needed/working!? Is this caused by the behaviour of the mail program?
You might be on to something there; see the sendwait option in the man page. systemd does kill remaining processes in the cgroup, so that would explain it. Try with this option enabled.
Offline
Just in the phone. Have you tried the "oneshot" service type?
Every time I see some piece of medical research saying that caffeine is good for you, I high-five myself. Because I'm going to live forever. -- Torvalds, Linus (2010-08-03).
Offline
You might be on to something there; see the sendwait option in the man page. systemd does kill remaining processes in the cgroup, so that would explain it. Try with this option enabled.
Thanks, that helped. I could not find a sendwait option but man systemd.kill led me to KillMode=process. Indeed, using a combination of watch and systemd-cgls I figured out that mail is processing the email in the background (I guess you could say asynchronously), so my script exits before the email processing is finished. By default (KillMode=control-group), systemd kills all remaining processes in the control group.
"oneshot" does not help here but thanks for the suggestion.
Best
Patrick
Offline
Thanks, that helped. I could not find a sendwait option
Really? You are using s-nail? Or something else that provides the mail command? (pkgfile doesn't show any other providers in the official repositories.) That man page also explicitly states mail is sent asynchronously.
Changing the killmode without setting this option does prevent killing a forked process, but it is really more of a workaround. With sendwait it should work better with systemd: mail should exit after sending has finished and it can then also report a failure to send through a non-zero exit status that you can see with `systemctl status`, for example.
Offline
Completely misunderstood you... I was looking for sendwait in systemd. I have found it now and yes, the manual is actually quite explicit about the asynchronous nature of mail. Added `-Ssendwait` to the mail command and now it is working as expected (without any systemd workarounds). So this is the proper way I was looking for but nevertheless I'm glad to have learned about systemd's behaviour with regard to cgroups. Thanks again!
Offline
I've stumbled across the same issue today as I created a service unit for my backup. A simple script using duplicity and mailing the result afterwards. I had to change the KillMode to process, because with the default, it was not working. Perhaps this has to do with the way systemd tries to guess the main pid. Or it is trying to guess even for oneshot and simple services.
Offline
Pages: 1