Everyone Can Learn Program
中文 Start learning
Core · LinuxLesson 5 of 6Free to read

Processes and services · Linux and the Command Line

How do you see what's running and restart what broke?

Course contents

Why you need it

"The site is down" usually means a program stopped. On a server, long-running programs are services managed by systemd. Knowing how to check a service, read why it failed, fix it and start it again is the core of being on call.

The intuition

Every running program is a process with a number, its PID. ps lists them; kill PID asks one to stop.

A service is a process systemd looks after:

  • systemctl status web: running or not, and the last log lines
  • sudo systemctl start|stop|restart web: change it (needs sudo)
  • sudo systemctl enable web: start it automatically after a reboot
  • journalctl -u web: the service's full log

The routine is always the same: status → log → fix → start → status again.

Definition

Process and PID

A running program and its process id. The same program started twice is two processes with two PIDs.

Service (systemd unit)

A program systemd starts, restarts when it crashes, and can start at boot. Its state is active, inactive or failed.

Examples

Check a healthy service

Run it in the lesson

The web server is running: ps shows its process and status says active.

ps
systemctl status web
Step by step
  1. L1web is PID 1201.
  2. L2Active: active (running), enabled at boot.

Fix a failed service

Run it in the lesson

The database failed. Its log says why: a config file is missing. Create it, then start the service.

systemctl status db
journalctl -u db
sudo touch /etc/db.conf
sudo systemctl start db
systemctl status db
Step by step
  1. L1Active: failed.
  2. L2The log names the missing file.db: config file /etc/db.conf not found
  3. L3/etc belongs to root, so creating files there needs sudo.
  4. L5Active: active (running).

Common mistakes

Restarting without reading the log

If the cause is still there, the service fails again, and you have lost time. Read journalctl -u first.

start is not enable

start runs it now; enable makes it start after a reboot. A server that reboots at night comes back without the service unless it is enabled.

Quick quiz

1. `sudo systemctl start db` fails with "Job for db.service failed". What next?

  1. ARun the same start command again
  2. BRead `journalctl -u db`
  3. Cchmod 777 /etc
Show the answer

Answer: B

The log says why it failed. Repeating the command or removing protections won't fix the cause.

2. What does this print?

sudo systemctl stop web
systemctl status web
  1. A
    ● web.service - Shop web server
         Loaded: loaded (/etc/systemd/system/web.service; enabled)
         Active: inactive (dead)
      Started Shop web server.
      Stopped Shop web server.
  2. B
    ● web.service - Shop web server
         Loaded: loaded (/etc/systemd/system/web.service; disabled)
         Active: inactive (dead)
      Started Shop web server.
      Stopped Shop web server.
  3. C
    ● web.service - Shop web server
         Loaded: loaded (/etc/systemd/system/web.service; enabled)
         Active: active (running)
      Started Shop web server.
      Stopped Shop web server.
Show the answer

Answer: A

stop makes it inactive now, but it stays enabled, so it would start again after a reboot.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictStop a process

    After these commands, what is true?

  2. ModifyBring the database back

    These commands try to start db, but they fail. Fix them so the database is running and starts after a reboot.

  3. WriteA safe restart

    Restart the web service, then show its status so you can see it worked. Use sudo only where it is needed.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.

Terms in this lesson

TermChineseDefinition
Process进程A running program, identified by its PID.
Service服务A long-running program managed by systemd.
On call值班Being the person who responds when production breaks.