Chapter 2: Command-line essentials

Exercise: Command-line essentials review

A service on servera stopped overnight, and a colleague has copied the end of its log to your workstation. Investigate the log from the command line, gather four facts into files, and let lab grade check them.

This lab uses everything in the chapter. It also uses one thing chapter 5 explains in depth: > after a command saves what the command prints into a file instead of showing it. For example, date > now.txt writes the date into now.txt.

Command-line essentials

  • Read the start and end of a log, identify files and count lines.
  • Run a command on another machine over SSH.
  • Save command output to files, using history and completion to type less.

Before you start: complete the practice lab and this chapter's lessons. Everything runs as student on workstation, in ~/sa-cli-review. The lab changes no server, so there is nothing to reset afterwards, but lab start expects clean servers anyway, as every exercise does.

In ~/sa-cli-review, create these four files:

  • time.txt: the current time on workstation, on a 24-hour clock (HH:MM).
  • last5.txt: exactly the last five lines of server.log.
  • lines.txt: the number of lines in server.log, as wc -l prints it.
  • servera-kernel.txt: the kernel release servera is running (uname -r prints it).

Then grade your work with lab grade sa-cli-review, and read the five log lines: what went wrong?

Start the exercise

On workstation, as student:

[student@workstation ~]$ lab start sa-cli-review
==> ~/sa-cli-review is ready: cd ~/sa-cli-review
  Start from clean servers (on the Ubuntu host: rht-vmctl reset servers).
[student@workstation ~]$ cd ~/sa-cli-review
[student@workstation sa-cli-review]$

cd changes your current directory; chapter 3 covers it. Notice that the prompt now shows sa-cli-review instead of ~.

Identify the two files you were given

What kind of files are README and server.log? Read the README.

[student@workstation sa-cli-review]$ file README server.log
README:     ASCII text
server.log: ASCII text
[student@workstation sa-cli-review]$ cat README
Exercise: sa-cli-review (chapter 2 lab)
...output omitted...

Both are plain text, so cat and less can show them. file accepts several file names at once.

Save the time

Save the current time on a 24-hour clock into time.txt, and check the file.

[student@workstation sa-cli-review]$ date +%R > time.txt
[student@workstation sa-cli-review]$ cat time.txt
09:44

With >, date prints nothing on the screen: its output went into the file.

Save the last five lines of the log

Save exactly the last five lines of server.log into last5.txt. Type server.log with Tab completion.

[student@workstation sa-cli-review]$ tail -n 5 server.log > last5.txt
[student@workstation sa-cli-review]$ cat last5.txt
Oct  3 08:58:12 servera inventory-api[2214]: database connection lost: timed out after 30s
Oct  3 08:58:12 servera inventory-api[2214]: retrying in 5s (attempt 1 of 3)
Oct  3 08:58:17 servera inventory-api[2214]: retrying in 5s (attempt 2 of 3)
Oct  3 08:58:22 servera inventory-api[2214]: retrying in 5s (attempt 3 of 3)
Oct  3 08:58:27 servera systemd[1]: inventory-api.service: Main process exited, code=exited, status=1/FAILURE

The story is in those five lines: the service lost its database connection, retried three times, and then exited with an error.

Save the line count

Save the number of lines in server.log into lines.txt, exactly as wc -l prints it. Reuse the file name with Alt+.: the last word of your previous command was last5.txt, so press it twice to reach server.log, or edit the previous command instead.

[student@workstation sa-cli-review]$ wc -l server.log > lines.txt
[student@workstation sa-cli-review]$ cat lines.txt
40 server.log

Save servera's kernel release

ssh can run a single command on another machine and print its output here, without opening a shell: put the command after the host name. Save servera's kernel release into servera-kernel.txt.

[student@workstation sa-cli-review]$ ssh servera uname -r > servera-kernel.txt
[student@workstation sa-cli-review]$ cat servera-kernel.txt
5.14.0-687.53.1.el9_8.x86_64

uname -r ran on servera; its output travelled back over SSH and > saved it on workstation. Your release may be newer.

Check and finish

Check your work with lab grade sa-cli-review, then put the exercise away with lab finish sa-cli-review.

[student@workstation sa-cli-review]$ lab grade sa-cli-review
sa-cli-review / final — read-only checks
PASS README: present
     Review: #/ch02/lab
PASS server.log: present
     Review: #/ch02/lab
PASS time.txt holds a 24-hour time such as 09:30
     Review: #/ch02/lab
PASS last5.txt holds exactly the last five lines of server.log
     Review: #/ch02/lab
PASS lines.txt shows the line count as wc -l prints it
     Review: #/ch02/lab
PASS servera-kernel.txt holds servera's kernel release
     Review: #/ch02/lab
Grading only reads. Fix any FAIL and grade again; for lasting changes, reboot the server and grade once more.