Skip to content

Block C: processes and systemd (17-25)

Task 17: find a process

Session 1: sleep 600. Session 2:

ps -C sleep -o pid,user,etime,cmd

Task 18: stop it, then a background process

kill <PID>          # from session 2, with the exact number
sleep 600 &         # in the background
echo "terminal is free"; jobs -l

⚠️ We left <PID> as text and got syntax error near unexpected token: < and > are redirections for the shell, so nothing on the line ran.

Task 19: ps, top, kill

ps -p <PID> -o pid,user,stat,%cpu,%mem,rss,etime,cmd
top -b -n 1 -p <PID>
kill <PID>; sleep 1; ps -p <PID> || echo "process is gone"

✅ STAT S (sleeping), %CPU 0.0, RES about 8 MiB; after kill the process is gone.

💡 Interactive top takes over the screen (quit with q); -b -n 1 prints plain text.

🔴 kill sends TERM. kill -9 is the last resort. pkill sleep would stop every sleep.

Task 20: your own service

command -v sleep
sudo nano /etc/systemd/system/linux-practice.service
[Unit]
Description=Linux practice service

[Service]
Type=simple
User=<user>
ExecStart=/usr/bin/sleep 3600
Restart=no

[Install]
WantedBy=multi-user.target

Task 21: start and check

sudo systemctl daemon-reload && sudo systemctl start linux-practice.service
systemctl status linux-practice.service --no-pager
pid=$(systemctl show -p MainPID --value linux-practice.service); ps -p $pid -o pid,user,cmd

✅ active (running), non-zero MainPID, owned by your user.

Task 22: stop, start, restart

s=linux-practice.service
sudo systemctl stop $s; systemctl is-active $s
sudo systemctl start $s; systemctl is-active $s; systemctl show -p MainPID --value $s
sudo systemctl restart $s; systemctl is-active $s; systemctl show -p MainPID --value $s

✅ inactive, active, active with a new PID.

Task 23: enable and disable

sudo systemctl enable $s;  echo "enabled: $(systemctl is-enabled $s) | active: $(systemctl is-active $s)"
sudo systemctl disable $s; echo "enabled: $(systemctl is-enabled $s) | active: $(systemctl is-active $s)"

✅ enable creates a symbolic link in multi-user.target.wants, disable removes it. active does not change.

Command What it changes
start / stop whether it runs now (active)
enable / disable whether it starts at boot (enabled)

Task 24: logs

sudo journalctl -u linux-practice.service --since "10 min ago" --no-pager
sudo journalctl -u linux-practice.service -f        # session 1, leave with Ctrl+C
sudo systemctl stop linux-practice.service; sudo systemctl start linux-practice.service   # session 2

✅ Session 1 showed Stopping, Stopped, Started.

💡 -- No entries -- is not an error: nothing was logged in the last 10 minutes.

Task 25: break it and fix it

flowchart LR
    A["copy to backups"] --> B["wrong ExecStart"] --> C["daemon-reload, restart"] --> D["status: failed"] --> E["journalctl: cause"] --> F["restore the copy"] --> G["status: running"]
cp /etc/systemd/system/linux-practice.service /srv/linux-practice/backups/linux-practice.service
sudo sed -i 's|^ExecStart=.*|ExecStart=/usr/bin/sleep-missing 3600|' /etc/systemd/system/linux-practice.service
sudo systemctl daemon-reload && sudo systemctl restart linux-practice.service
systemctl status linux-practice.service --no-pager
sudo journalctl -u linux-practice.service -n 10 --no-pager
sudo cp /srv/linux-practice/backups/linux-practice.service /etc/systemd/system/linux-practice.service
sudo systemctl daemon-reload && sudo systemctl restart linux-practice.service

✅ failed, status=203/EXEC, and in the log Unable to locate executable '/usr/bin/sleep-missing': No such file or directory. After restoring: active (running).

💡 status truncates lines, journalctl shows them in full.