Command Line Advanced: Scripting and Administration
Services, Logs and Disks
In this lesson you will
- Start, stop and enable a service with systemctl
- Read logs with journalctl
- Check disk space, memory and load
Introduction
Programs that must run all the time, such as a web server or database, are services (also called daemons). Most modern Linux distributions manage them with systemd. When a server misbehaves, you need to check the service, its logs and the machine's resources.
Managing services
Terminal
systemctl status nginx # is it running? recent log lines
sudo systemctl start nginx # start now
sudo systemctl stop nginx # stop now
sudo systemctl restart nginx # stop and start
sudo systemctl reload nginx # reread config without stopping, if supported
sudo systemctl enable nginx # start automatically at boot
sudo systemctl disable nginx
systemctl list-units --type=service --state=failedstart and enable are different. One starts the service now, and the other makes it start at boot.
Quick quiz
What does systemctl enable do?
Writing a service
A minimal unit file, saved as /etc/systemd/system/myapp.service:
[Unit]
Description=My application
After=network.target
[Service]
ExecStart=/usr/bin/node /srv/myapp/server.js
Restart=on-failure
User=myapp
[Install]
WantedBy=multi-user.targetThen load and start it:
Terminal
sudo systemctl daemon-reload
sudo systemctl enable --now myappRun services as a dedicated, unprivileged user, not as root.
Reading logs
On systemd systems, journalctl shows logs:
Terminal
journalctl -u nginx # logs for one service
journalctl -u nginx -f # follow live
journalctl -u nginx --since "1 hour ago"
journalctl -p err -b # errors since this bootTraditional logs also live in /var/log/, such as /var/log/syslog or /var/log/auth.log. Combine them with the text tools you know: grep "error" /var/log/syslog | tail.
Disk space
Terminal
df -h # space on each filesystem, human readable
du -sh /var/log # size of one folder
du -h --max-depth=1 /var | sort -h # biggest folders in /varA full disk breaks many things, and logs are a common cause. Check df -h early when something odd happens.
Memory and load
Terminal
free -h # memory and swap
uptime # load averages over 1, 5 and 15 minutes
top # what is using CPU and memoryLoad average roughly counts processes waiting to run. As a rule of thumb, a load persistently above the number of CPU cores means the machine is overloaded.
A troubleshooting routine
- Is the service running?
systemctl status - What do the logs say?
journalctl -u name -n 50 - Is the disk full?
df -h - Is memory or CPU exhausted?
free -h,top - Is the network reachable?
ping,curl - What changed recently? Think about deployments and updates.
Change one thing at a time, and note what you did.
Try it yourself
On a Linux machine or virtual machine, list failed services, view the last 20 lines of a service's log, and find the three largest folders under /var.
Summary
systemctlstarts, stops and enables services, and unit files define them.journalctland/var/loghold logs, andgrephelps search them.df,du,freeanduptimeshow disk, memory and load for diagnosis.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading