Ubuntu periodic tasks
Ubuntu is an open-source system that allows users to control different components. Default desktop installation has a bunch of periodic tasks out of the box. It’s important to understand what is the purpose of these tasks and how often they are called. This knowledge allows to predict the network activity, reduce the number of unexpected CPU spikes and improve user experience. Moreover, some of the default tasks can be redundant for the particular user and that is why should be disabled.
Ubuntu 21.04 on my laptop uses systemd as a
core component, so periodic jobs are implemented as
timer units. Timers mechanism is an alternative
for the cron, the most well-known job scheduler. For
backward compatibility, cron is also installed into default Ubuntu distribution. It’s started by the
cron.service on a boot time. Systemd timers are more flexible than cron jobs. It’s possible to
point out such advantages of systemd timers:
- easier debugging with verbose logging in systemd journal;
- advanced resource management as for any other systemd service: syscall filtering, IO scheduling, etc;
- possibility to define the relationships between the timer and any other systemd service, e.g.
forbid parallel execution with another service using Conflictsinstruction;
- systemd timer can be activated not only at the specified time but also by the complex conditions, like a minute after some hardware had been plugged.
The command systemctl list-timers lists all active timers in the system. A fresh Ubuntu 21.04
installation gives such output:
logrotate.timer
fstrim.timer
systemd-tmpfiles-clean.timer
e2scrub_all.timer
man-db.timer
ua-messaging.timer
apt-daily.timer
apt-daily-upgrade.timer
update-notifier-download.timer
update-notifier-motd.timer
fwupd-refresh.timer
motd-news.timer
anacron.timer
The timer is defined by the corresponding file in the /usr/lib/systemd/system directory. It
describes when the process should be started. Each timer has a service that describes what process
and how should be started. Almost any file in the system belongs to a particular package. It’s
possible to find the package using the command dpkg -S file-name.
The timers described above can be split into several groups. Detailed information on each of them is represented below.
Regular maintenance jobs
Every system needs some maintenance from time to time. It allows to check integrity, increase performance and reduce disk usage. Here are the timers responsible for such jobs:
- logrotate.timerallows automatic rotation, compression, removal, and mailing of log files once a day;
- fstrim.timerdiscards (or “trim”) blocks that are not in use by the filesystem once a week; it’s useful for solid-state drives (SSDs);
- systemd-tmpfiles-clean.timer— daily cleanup of temporary directories;
- e2scrub_all.timer— periodic ext4 online metadata check for all filesystems;
- man-db.timer— daily man-db regeneration.
I don’t fully understand the purpose of the man-db.timer: it’s better to regenerate man-db indexes
by dpkg when a new package is being installed, that is why additional periodic updates look
redundant.
Ubuntu Advantage
ua-messaging.timer is a part of ubuntu-advantage-tools package — management tools for
Ubuntu Advantage, the paid support for enterprises. The timer starts
updating the messaging text for use in MOTD and APT custom Ubuntu Advantage messages.
The package is installed by default. From the description of the package, I’d found that it provides
users with a simple mechanism to view, enable, and disable offerings from Canonical on their system.
I don’t use Canonical paid services on my laptop, that is why I prefer to delete the package with
apt purge ubuntu-advantage-tools command.
Software updates
A bunch of timers is responsible for automatic software updates:
- apt-daily.timerchecks for updates and downloads new packages;
- apt-daily-upgrade.timerinstalls new packages without interaction with user;
- update-notifier-download.timerdownload data for packages that failed at package install time;
- update-notifier-motd.timerchecks if new release available.
On the one side, it’s important to keep the system up to date, because of security issues. On the
other side, auto-updates can cause unexpected network traffic, poor user experience (like the
requirement to restart Firefox with a lot of opened tabs after background update), or even system
instability. I prefer to remove update-notifier-common package, which is responsible for
auto-updates, and execute command apt update && apt upgrade several times a week by hands only
when I’m ready for updates.
Firmware update
Modern hardware is sophisticated and often has internal firmware that needs to be updated from time to time to improve the user experience, increase stability and fix security issues. Updating firmware on devices is difficult for Linux users due to the lack of Windows-specific flashing tools. That is why Linux Vendor Firmware Service (LVFS) was introduced. It allows hardware vendors to upload firmware updates that will be used by all major Linux distributions.
fwupdmgr is a client for LVFS. fwupd-refresh.timer is a timer
that downloads the latest metadata from LVFS regularly using fwupdmgr. It’s an important process and
should be done from time to time but I prefer to do it by hand. That is why I choose to disable
this timer with such commands:
sudo systemctl stop fwupd-refresh.timer
sudo systemctl disable fwupd-refresh.timer
motd news
There is a message displayed on different Unix-like systems on the login called “message of the day”
(motd). On Ubuntu, a part of motd is a news message
from https://motd.ubuntu.com. According to /etc/update-motd.d/50-motd-news file the system sends a
request to the Ubuntu server containing information about release version, kernel version, CPU
architecture, and cloud ID, so the server can send messages that are relevant to a particular
distribution or hardware. The typical message looks like this:
 * Super-optimized for small spaces - read how we shrank the memory
   footprint of MicroK8s to make it the smallest full K8s around.
   https://ubuntu.com/blog/microk8s-memory-optimisation
The idea is to show the user some useful information on login looks reasonable, but I don’t like when the system sends some data about my configuration to an external server from time to time even if it’s a server of Canonical. Also, the messages from https://motd.ubuntu.com look like tech advertising rather than useful info.
The /etc/update-motd.d/50-motd-news file belongs to one of the core packages of the system —
base-files, that is why it’s difficult to delete it safely. Fortunately, this feature is disabled
for desktops and no external requests are being sent. Also, I turn off this timer with such
commands:
sudo systemctl stop motd-news.timer
sudo systemctl disable motd-news.timer
anacron
Anacron is a tool that is used to execute
commands with a frequency specified in days. I’d checked /etc/anacrontab and found three groups of
tasks:
- /etc/cron.daily;
- /etc/cron.weekly;
- /etc/cron.monthly.
There are no weekly and monthly tasks, but /etc/cron.daily directory contains several files. Some
of tasks have systemd timers equivalents:
- apt-compat->- apt-daily.timer;
- logrotate->- logrotate.timer;
- man-db->- man-db.timer.
These files contain a check for systemd existence and if it’s found — tasks do nothing and exit.
Also, there are some maintenance tasks:
- apportcleans all crash reports that are older than a week;
- dpkgbackups the 7 last versions of dpkg databases containing user data, so it’s possible to recover in case of corruption.
- cracklib-runtimeupdates cracklib dictionaries if required. For me such decision looks a bit strange: why don’t update dictionaries in case of deb-package upgrade instead of running the same task periodically without any data changes?
Also, I use some third-party software: Google Chrome and Slack. I found the tasks for each of them:
/etc/cron.daily/google-chrome and /etc/cron.daily/slack. Each piece of software creates the
repository configuration file for the package updates. The idea of the periodic task is monitoring
that config to see if it has been disabled by the overly aggressive distro upgrade process. When
this situation is detected, the repository will be re-enabled.
Conclusion
Linux is a complicated system. It can be configured in different ways. Ubuntu can provide the defaults that look reasonable for maintainers but can be a bit controversial for a particular user. The only option to find the best decision is a system exploration. The user should understand what some particular process does. Only in this case he can decide is it suitable for his setup or it’s better to make some changes.
In the article, I’d described the decisions I’d made for my setup and why. Some people will agree with them and others will not. It’s OK. The idea of the article is not to suggest some particular solution but to encourage users to explore the system and make their own meaningful choice.