Skip to content

Linux & shell: Logs

Linux & shell commands for logs (tags: #logs #cloudwatch).

Index
6 items

Linux: Follow logs of a systemd service

Safe

Stream a service's journal, starting with the last 100 lines.

journalctl -u <service> -f -n 100
What each part does
  1. journalctlread the systemd journal
  2. -u <service>only this unit
  3. -ffollow new lines
  4. -n 100start with the last 100 lines
Example
$ journalctl -u nginx -f -n 100
Nov 20 09:12:01 web-1 nginx[812]: 10.0.3.4 - - "GET /healthz HTTP/1.1" 200

My note Add --since "10 min ago" when the service is chatty.

Also known as: journalctl · systemd logs · service logs

Linux: Show errors since last boot

Safe

Only priority err and worse from the current boot: fastest way to see what's wrong on a box.

journalctl -b -p err --no-pager
What each part does
  1. journalctlread the systemd journal
  2. -bthis boot only (-b -1 for the previous one)
  3. -p errpriority err and worse (crit, alert, emerg)
  4. --no-pagerprint directly

Also known as: boot errors · journalctl -p err

Linux: Follow a log file across rotation

Safe

Like tail -f but keeps following when logrotate replaces the file.

tail -F -n 200 <file>
What each part does
  1. tailprint the end of a file
  2. -Ffollow by name and retry: keeps going when the file is rotated or recreated (-f would stop)
  3. -n 200start with the last 200 lines
  4. <file>the log file

My note -F (capital) = --follow=name --retry. -f stops after rotation.

Also known as: tail -f · follow log

Linux: Search a log for an error with context

Safe

Case-insensitive search with line numbers and 3 lines of context around each hit.

grep -n -i -C 3 "<pattern>" <file>
What each part does
  1. grepsearch text
  2. -nshow line numbers
  3. -icase-insensitive
  4. -C 33 lines of context before and after each match
  5. "<pattern>" <file>what to look for, and where
Example
$ grep -n -i -C 3 "timeout" /var/log/app/app.log

Also known as: grep context · find error in log

Linux: Vacuum old journal logs

Caution

Delete archived journal files older than N days to reclaim disk.

sudo journalctl --vacuum-time=<days>d
What each part does
  1. sudo journalctlmanage the journal as root
  2. --vacuum-time=<days>ddelete archived journal files older than N days (or --vacuum-size=500M)

My note Check size first: journalctl --disk-usage

Also known as: journal too big · /var/log/journal

Linux: Delete old rotated logs

Destructive

Remove compressed rotated logs older than N days. Run without -delete first to preview.

sudo find /var/log -type f -name "*.gz" -mtime +<days> -delete
What each part does
  1. sudo find /var/logsearch under /var/log as root
  2. -type ffiles only
  3. -name "*.gz"compressed rotated logs
  4. -mtime +<days>last modified more than N days ago
  5. -deletedelete matches (run without it first to see the list)

Also known as: clean /var/log · no space left on device

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…