My Server Starts too Slowly

There are several reasons why a server or desktop Linux system may be starting slowly. There are several commands to help understand where to look. In this presentation, we focus on systemd and systemctl to control and status services.

To see your version of systemctl:
sudo systemctl --version

To analyze your total startup time:
sudo systemd-analyze

To see the specific services taking the longest to start:
sudo systemd-analyze blame

To look at the status of all services:
sudo systemctl status

To list which services had problems starting:
sudo systemctl --failed

We installed the apache web server to see examples of how to control a service:
sudo apt install apache2

Commands to control the service:

sudo systemctl status apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl start apache2

Most services are defined in:
/usr/lib/systemd/system

Other services are defined in:
/etc/systemd/system

We defined a simple service to run a script.
sudo nano /etc/systemd/system/rc-local.service

Insert the following in the file:

[Unit]
Description=/etc/rc.local Service
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

Next create the script:
sudo nano /etc/rc.local

Insert the following in the script:

#!/bin/sh -e
sudo date >/home/scott/booted.txt
exit 0

Set the execute privilege on the script file:
sudo chmod +x /etc/rc.local

Enable the service to start when the system boots:
sudo systemctl enable rc.local

To disable the service so that it will not start when the system boots:
sudo systemctl disable rc.local

As a bonus, I presented a way to speed up your system shutdown by making a backup copy and editing this file:

sudo cp /etc/systemd/system.conf /etc/systemd/system.conf.orig
sudo nano /etc/systemd/system.conf

Search for “DefaultTimeoutStopSec”. On my system, it was set to 90 seconds. I set it to 2s and now my Shutdown – Poweroff from the menu is much faster.