Fetchmail usually runs either as a single system wide daemon, or it's started by a user's cron job. But as you can see, the above fetchmail uses IMAP4's IDLE feature. So it has to keep a connection to the server open. Fetchmail can do this for only one connection at a time.
To me, that is enough, but I want my personal fetchmail instance to always run, not merely when I'm logged in. Luckily, systemd has a user feature that does exactly that.
Systemctl talks to DBus.
When calling systemctl through sudo for another user, there may be a bit of a problem because XDG_RUNTIME_DIR is not set properly.
We can easily solve this in ~/.bashrc
:
<snip> export XDG_RUNTIME_DIR="/run/user/$(id -u)"
Now when root sudos -s to become apprentice, it won't get
Failed to connect to bus: No such file or directory
<title>
Config
</title>
Create a systemd unit in ~cpp/.config/systemd/user/fetchmail.service
:
[Unit] Description="Fetchmail Program" [Service] Environment="XDG_RUNTIME_DIR=/run/user/%U" Environment="HOME=%h" ExecStart=/usr/bin/fetchmail Restart=always Type=simple # PIDFile Not needed #PIDFile="%h/.fetchmail.pid" [Install] WantedBy=default.target
Also check the config file for errors:
systemd-analyze verify ~cpp/.config/systemd/user/fetchmail.service
systemd validate ~cpp/.config/systemd/user/fetchmail.service
Shown here is how to do it from the root account. But the user can also do it himself. XDG_RUNTIME_DIR here still has to be set from the command line because sudo doesn't use Bash:
sudo -u apprentice XDG_RUNTIME_DIR=/run/user/$(id -g apprentice) systemctl --user enable fetchmail
Note | |
---|---|
This command creates a further structure under |
apprentice@host:~$ systemctl --user start fetchmail