Processes and services · Linux and the Command Line
How do you see what's running and restart what broke?
Course contentsWhy 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 linessudo systemctl start|stop|restart web: change it (needs sudo)sudo systemctl enable web: start it automatically after a rebootjournalctl -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 lessonThe web server is running: ps shows its process and status says active.
ps
systemctl status web- L1web is PID 1201.
- L2Active: active (running), enabled at boot.
Fix a failed service
Run it in the lessonThe 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- L1Active: failed.
- L2The log names the missing file.
db: config file /etc/db.conf not found - L3/etc belongs to root, so creating files there needs sudo.
- 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?
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 webShow 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.
- PredictStop a process
After these commands, what is true?
- 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. - WriteA safe restart
Restart the
webservice, 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
| Term | Chinese | Definition |
|---|---|---|
| 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. |