Skip to content
KALANBLON
Show contents

Chapter 4 of 6 · 20 min

Services, logs and scheduled tasks

By the end of this chapter, you'll be able to

  • Start, stop and enable a service with systemctl
  • Read and filter a service's logs with journalctl
  • Schedule a recurring task with cron
  • Monitor the server's disk space and accounts

Services with systemd

On most current Linux distributions (Debian, Ubuntu, Fedora…), programs that run permanently, such as a web server or a database, are services managed by systemd. They are controlled with systemctl (administrator rights are needed, hence sudo):

sudo systemctl status nginx       # service state: active? since when? last log lines
sudo systemctl start nginx        # start now
sudo systemctl stop nginx         # stop now
sudo systemctl restart nginx      # stop then start again (after a configuration change)
sudo systemctl enable nginx       # start automatically when the server boots
sudo systemctl disable nginx      # no longer start automatically
systemctl --failed                # list failed services

Don't confuse start (now) and enable (at every server boot): a service that is started but not enabled does not come back after a reboot. sudo systemctl enable --now nginx does both at once.

The output of status looks like this:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Sat 2026-11-14 23:12:04 UTC; 1 day 9h ago
    Process: 812 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)

Three pieces of information: enabled (the service starts with the server), failed (it stopped on an error), and the date it stopped.

Logs with journalctl

systemd stores the messages of all services in a journal. journalctl reads and filters them:

journalctl -u nginx                       # all messages of the nginx service
journalctl -u nginx --since "2026-11-14 23:00" --until "2026-11-14 23:30"
journalctl -u nginx -p err                # errors only
journalctl -u nginx -f                    # follow live (Ctrl+C to exit)
journalctl --disk-usage                   # space taken by the logs

In our example, Saturday evening's log would say for instance:

Nov 14 23:12:04 server nginx[812]: nginx: [emerg] unknown directive "server_nam" in /etc/nginx/sites-enabled/school:3

A typo (server_nam) in a configuration file changed on Saturday: nginx refuses to start. We fix it, check the configuration with sudo nginx -t, then sudo systemctl restart nginx.

The service restarts; enable guarantees it will come back after a server reboot.

Step 5 of 5
Text description of the animation
  1. systemctl status shows that nginx stopped on an error on Saturday evening.
  2. journalctl -u nginx --since Saturday gives the exact cause, with the file and the line.
  3. We fix the typo on line 3 of the indicated file.
  4. nginx -t checks the configuration without stopping anything: never restart without this test.
  5. The service restarts; enable guarantees it will come back after a server reboot.

Scheduling with cron

cron runs commands at fixed times. Each user has their own crontab, edited with crontab -e (and shown with crontab -l). A line contains five date fields, then the command:

# minute  hour  day-of-month  month  day-of-week  command
  30      2     *             *      *            /usr/local/bin/backup.sh
  */15    *     *             *      *            /usr/local/bin/check-disk.sh
  0       8     *             *      1-5          /usr/local/bin/report.sh
  • 30 2 * * *: every day at 2:30;
  • */15 * * * *: every 15 minutes;
  • 0 8 * * 1-5: at 8:00, Monday (1) to Friday (5).

cron runs commands with a minimal environment: use full paths, and redirect the output to a file to keep a trace, for example >> /var/log/backup.log 2>&1.

Monitoring disk and accounts

Two checks to do regularly:

df -h /                     # disk space of the main partition
du -sh /var/log/* | sort -h | tail -5    # the 5 largest items in /var/log
who                         # who is logged in right now
last -n 10                  # the last 10 logins
sudo adduser fatou          # create an account (Debian, Ubuntu)
sudo usermod -aG www-data fatou          # add fatou to the www-data group

df reads the real free space; here is how a script can extract the used percentage from df output and raise an alert:

df_output="Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 44G 6G 88% /"
used=$(echo "$df_output" | awk 'NR == 2 { gsub("%", "", $5); print $5 }')
if (( used >= 85 )); then
  echo "ALERT: disk used at $used%"
fi
ALERT: disk used at 88%

On a real server, replace the first line with df_output=$(df -h /). awk selects the second line (NR == 2), removes the % from the fifth column and prints it.

Mini-lab: analysing a log fileTerminal simulator

Terminal

Click “Run”: the commands run in a fresh terminal, in your home folder /home/awa. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Linux commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: the right administration commands

Give the command or the cron line for each need:

1. See whether the postgresql service is active, and since when.
2. Make postgresql start automatically at server boot, and start it now.
3. Show postgresql's errors since yesterday.
4. Run /usr/local/bin/cleanup.sh every Sunday at 3:00.
5. Run /usr/local/bin/check.sh every 10 minutes, keeping its output in /var/log/check.log.
Hint 1
journalctl accepts --since yesterday.
Hint 2
In cron, Sunday is day 0 (or 7).
Show the solution and common mistakes
1. systemctl status postgresql
2. sudo systemctl enable --now postgresql
3. journalctl -u postgresql -p err --since yesterday
4. 0 3 * * 0 /usr/local/bin/cleanup.sh
5. */10 * * * * /usr/local/bin/check.sh >> /var/log/check.log 2>&1

Common mistakes:

  • Writing * 3 * * 0 for the Sunday task: with a star in the minutes field, the script runs 60 times, from 3:00 to 3:59.
  • Forgetting 2>&1: error messages do not go into the file, and a failure of the task goes unnoticed.

Summary

  1. 1systemctl start, stop and restart act now; enable makes a service start with the server.
  2. 2journalctl -u service reads a service's log, filterable by date (--since) and severity (-p err).
  3. 3Test a configuration (nginx -t…) before restarting, and prefer reload when possible.
  4. 4A cron line has five date fields then the command, with full paths and a redirection.
  5. 5df, du, who and last monitor disk space and logins.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.