Technical skill

Linux command: other

Subject: .../LINUX

Find a useful message in a long log

Small command-line tools can answer questions about files, text, and running programs. Because each tool does one job, you can combine a few instead of reading a long file by hand. Imagine an application has been rejecting sign-ins. Its log is a record of events and messages and may be too long to read line by line. On some Linux systems, systemd manages background services and a journal records their messages. journalctl reads that journal and can select messages for one service. A pipe, written as |, passes one command's output to the next command, so a few tools can filter the log in stages:

journalctl -u myapp.service --since today |
  grep -i -- 'authentication failed' |
  tail -n 20

The first command selects the myapp.service log entries from today. grep keeps messages that contain the words, and -i ignores letter case. tail -n 20 shows the last twenty matches. If the system does not use systemd, read the service's configured log files instead. For a plain file, grep -n adds line numbers; tail -F keeps following the file if it is replaced during log rotation.

Combine tools for smaller questions

Each command reads input and writes output. The shell connects one program's standard output, its usual results, to the next program's standard input, where it receives data. cat prints a short file; less lets you page through or search a longer result. head shows the start of a stream, while tail shows its end. With grep, -F treats a search pattern as plain text instead of special pattern syntax, and -i ignores case.

For a quick view of CPU use, ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head puts high-CPU processes first on procps-ng systems. It shows a single moment, not an ongoing monitor. ps supports several option styles across Unix systems, so check the local manual before copying the command elsewhere.

Other common tools count and arrange text. wc -l counts lines, sort orders them, and uniq -c counts repeated neighboring lines. uniq only checks adjacent lines, so sort the input first when duplicates may be separated. cut selects fields divided by a chosen separator. For recurring work, put a long pipeline in a script with named variables and comments. Quote patterns and values so spaces stay in one argument and wildcard characters do not expand by accident. If a value can begin with a hyphen, use -- when the command supports it, or start a path with ./.

Check whether the whole pipeline worked

Commands report a status when they finish. In Bash, a pipeline normally reports the status of its last command; set -o pipefail makes it fail when an earlier command fails too. This setting is Bash-specific. On a systemd host, systemctl status myapp.service shows the service's state and journalctl -u myapp.service shows its messages. A process can be running while the application still rejects requests, so check its output and try the operation a person needs.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful