Automation and Some Fun
Summary
This chapter explores ways we can use Linux to Automate our world.
Note that some of this content on PS1 has been covered in previous sections.
Learn to customize your terminal with colorful prompts using PS1 and ANSI color codes, display system information with neofetch, and discover classic Linux easter eggs like cowsay, figlet, and the infamous sl command. A personalized terminal makes daily work more enjoyable.
Concepts Covered
This chapter covers the following concepts from the learning graph:
- Automating Tasks
- Monitoring Tasks
- Logging Tasks
- Event Mining
- Terminal Customization
- Terminal Colors
- ANSI Color Codes
- Neofetch
- Cowsay
- Figlet
- Sl Command
- Linux Easter Eggs
Prerequisites
This chapter builds on concepts from:
- Chapter 2: Getting Started with the Terminal
- Chapter 11: Shell Configuration and Environment
- Chapter 14: Package Management
Welcome to the Fun Side of Linux!
You've spent a lot of time learning serious Linux skills—file management, permissions, networking, scripting. Now it's time to put those skills to work automatically AND add some personality to your terminal. Because what's the point of mastering Linux if you can't make it look cool and do your work for you while you sleep?
This chapter is split into two parts. First, we'll explore automation—making Linux do repetitive tasks without your constant supervision. Then we'll dive into the fun stuff: making your terminal look amazing and discovering some of the quirky easter eggs that Linux developers have hidden over the years.
Let's automate the boring stuff and beautify the rest!
Part 1: Automation - Let Linux Do the Work
The true power of Linux isn't just doing things—it's doing things automatically. A good sysadmin is a lazy sysadmin (in the best way!). Why manually check disk space every day when a script can do it and alert you only when there's a problem?
Automating Tasks with Cron
Cron is the Linux scheduling system that runs tasks automatically at specified times. The name comes from "chronos" (Greek for time), and it's been helping Linux users automate tasks since the 1970s.
Every Linux system has a cron daemon running in the background, constantly checking if any scheduled tasks need to run. You control it through a "crontab" (cron table)—a file that lists your scheduled tasks.
Viewing and Editing Your Crontab
1 2 3 4 5 6 7 8 | |
Cron Time Format
Cron uses a specific format for scheduling. Each line in your crontab has five time fields followed by the command:
1 2 3 4 5 6 7 | |
Here are some examples to make it click:
| Schedule | Cron Expression | Meaning |
|---|---|---|
| Every minute | * * * * * |
Runs constantly (usually not what you want!) |
| Every hour | 0 * * * * |
At minute 0 of every hour |
| Every day at 3 AM | 0 3 * * * |
Perfect for maintenance tasks |
| Every Monday at 9 AM | 0 9 * * 1 |
Weekly reports |
| First of month at midnight | 0 0 1 * * |
Monthly tasks |
| Every 15 minutes | */15 * * * * |
The / means "every" |
| Weekdays at 8 AM | 0 8 * * 1-5 |
The - defines a range |
Practical Cron Examples
Here are some real-world automation examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Percent Signs in Cron
In crontab, the % character has special meaning (it creates a newline). If you need a literal %, escape it with a backslash: \%. This is why $(date +\%Y\%m\%d) needs backslashes.
Diagram: Cron Scheduling Visualization
Cron Time Field Visualization
Type: microsim
Purpose: Interactive tool to help students understand and build cron expressions
Bloom Taxonomy: Apply (L3)
Learning Objective: Students will construct valid cron expressions by manipulating time fields
Canvas layout (800x500): - Top section: Five dropdowns/input fields for cron time fields - Middle section: Human-readable interpretation of the schedule - Bottom section: Preview of next 5 scheduled run times
Visual elements: - Five columns representing minute, hour, day, month, day-of-week - Each column shows current value with up/down arrows - Color coding: green for valid, red for invalid entries - Clock face showing when task will run - Calendar preview showing dates for monthly patterns
Interactive controls: - Dropdown or spinner for each time field - Quick preset buttons: "Daily at midnight", "Every hour", "Weekly", "Every 5 minutes" - Text input for direct cron expression entry - "Copy to clipboard" button
Default parameters: - Expression: 0 * * * * (every hour) - Shows next 5 runs based on current time
Behavior: - Real-time update of human-readable text as fields change - Highlight invalid combinations (e.g., day 31 in February) - Show warning for very frequent schedules - Calculate and display next run times
Implementation: p5.js with date/time calculation library
Monitoring Tasks
Setting up automation is only half the battle—you also need to know if your tasks are actually running! Monitoring ensures your automated systems are working correctly.
Checking Cron Logs
Cron logs its activities to the system log. Here's how to see what's been happening:
1 2 3 4 5 6 7 8 9 10 11 | |
Creating a Monitoring Script
Let's create a simple monitoring script that checks if your important services are running:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Then schedule it to run every 10 minutes:
1 | |
Logging Tasks
Good logging is essential for debugging automation issues. When something goes wrong at 3 AM, logs are your best friend for figuring out what happened.
Redirecting Output to Logs
Every cron job should log its output:
1 2 3 4 5 | |
Using the logger Command
The logger command writes messages to the system log (syslog), which is the proper place for system messages:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Creating Professional Log Entries
Here's a pattern for logging in your scripts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Event Mining: Finding Patterns in Logs
Event mining is the art of analyzing logs to find patterns, problems, and insights. With all those log files accumulating, how do you find what matters?
Basic Log Analysis
1 2 3 4 5 6 7 8 9 10 11 | |
Using journalctl for Systemd Logs
Modern Linux systems use systemd and journalctl for centralized logging:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Diagram: Log Analysis Workflow
Log Analysis and Event Mining Workflow
Type: workflow
Purpose: Show the process of analyzing logs to find actionable insights
Bloom Taxonomy: Analyze (L4)
Learning Objective: Students will analyze the log analysis workflow and understand how to extract meaningful patterns
Visual style: Flowchart with decision points
Steps: 1. Start: "Logs Generated" Hover text: "Applications, services, and system generate log entries"
-
Process: "Collect Logs" Hover text: "Centralize logs using syslog, journald, or log aggregators"
-
Process: "Filter by Severity" Hover text: "Focus on errors, warnings first: journalctl -p warning"
-
Decision: "Known Pattern?" Hover text: "Does this match a known issue or expected behavior?"
5a. Process: "Auto-Alert" (if known critical) Hover text: "Send notification for known critical patterns"
5b. Process: "Investigate" (if unknown) Hover text: "Use grep, awk, and analysis tools to understand"
-
Process: "Extract Patterns" Hover text: "Use awk, sort, uniq to find common occurrences"
-
Process: "Correlate Events" Hover text: "Match timestamps across different log sources"
-
Process: "Document Findings" Hover text: "Record what you learned for future reference"
-
Decision: "Action Needed?" Hover text: "Does this require intervention?"
10a. Process: "Create Fix/Alert" (if yes) Hover text: "Implement solution or create monitoring rule"
10b. End: "Archive for Reference" (if no) Hover text: "Keep for historical analysis"
Color coding: - Blue: Data collection steps - Yellow: Decision points - Green: Analysis steps - Orange: Action steps
Implementation: Mermaid flowchart or HTML/CSS/JS workflow
Creating Automated Alerts
Here's a script that monitors logs and sends alerts when it finds problems:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Part 2: Making Your Terminal Beautiful (and Weird)
Now for the fun part! Let's transform your boring terminal into something that reflects your personality. This isn't just about looking cool—a well-customized terminal can also be more informative and easier to use.
Terminal Customization Basics
Your terminal's appearance is controlled by several factors:
- PS1: The prompt string (what appears before your cursor)
- Colors: ANSI escape codes for text and background colors
- Terminal emulator settings: Font, window size, transparency
- Shell configuration: ~/.bashrc or ~/.zshrc files
Understanding ANSI Color Codes
ANSI escape codes are special character sequences that control terminal appearance. They might look like magic (or gibberish), but they follow a predictable pattern.
The basic format is:
1 | |
Where:
\eor\033= The escape character[= Start of the control sequenceCODE= One or more numbers (separated by;)m= End of the control sequence
Color Codes Quick Reference
| Code | Effect | Code | Effect |
|---|---|---|---|
| 0 | Reset all | 1 | Bold |
| 30 | Black text | 40 | Black background |
| 31 | Red text | 41 | Red background |
| 32 | Green text | 42 | Green background |
| 33 | Yellow text | 43 | Yellow background |
| 34 | Blue text | 44 | Blue background |
| 35 | Magenta text | 45 | Magenta background |
| 36 | Cyan text | 46 | Cyan background |
| 37 | White text | 47 | White background |
You can combine codes with semicolons:
1 2 3 4 5 6 7 8 | |
Always end with \e[0m to reset colors, or everything after will be affected!
Diagram: ANSI Color Code Chart
ANSI Color Code Interactive Reference
Type: microsim
Purpose: Interactive color picker showing ANSI codes and live preview
Bloom Taxonomy: Apply (L3)
Learning Objective: Students will apply ANSI color codes to create custom terminal colors
Canvas layout (800x500): - Left side (400px): Color palette grid - Right side (400px): Preview area and code output
Visual elements: - 8x2 grid for standard colors (text and background) - 8x2 grid for bright/bold variants - Large preview box showing sample text - Code output area with copyable text
Color grid: - Row 1: Standard foreground colors (30-37) - Row 2: Standard background colors (40-47) - Row 3: Bright foreground colors (90-97) - Row 4: Bright background colors (100-107)
Interactive controls: - Click color to select foreground - Shift+click to select background - Checkbox: Bold - Checkbox: Underline - Checkbox: Italic - Text input: Sample text to display - Button: "Copy Code"
Preview area: - Shows sample text with selected styling - Updates in real-time - Dark background option toggle
Output formats: - Echo command: echo -e "\e[1;31mText\e[0m" - PS1 format: [\e[1;31m]Text[\e[0m] - Bash variable: RED='\e[31m'
Default: - No selection (white text, no background) - Sample text: "Hello, Linux!"
Implementation: p5.js with clipboard API
Terminal Colors in Practice
Let's put those color codes to use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Customizing PS1 (Your Prompt)
The PS1 variable defines what your prompt looks like. By default, it's probably something boring like user@hostname:~$. Let's make it awesome!
PS1 Special Characters
| Code | Meaning |
|---|---|
\u |
Username |
\h |
Hostname (short) |
\H |
Hostname (full) |
\w |
Current directory (full path) |
\W |
Current directory (basename only) |
\d |
Date |
\t |
Time (24-hour) |
\T |
Time (12-hour) |
\n |
Newline |
\$ |
$ for regular user, # for root |
PS1 Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Testing PS1 Changes
Before adding a PS1 to your ~/.bashrc, test it by typing PS1='your_prompt_here' directly in the terminal. If something goes wrong, just close the terminal and open a new one!
Neofetch: System Info in Style
Neofetch displays system information alongside ASCII art of your distro's logo. It's perfect for screenshots and showing off your setup!
1 2 3 4 5 | |
You'll see something beautiful like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Neofetch Configuration
Neofetch is highly customizable. The config file is at ~/.config/neofetch/config.conf.
1 2 3 4 5 6 7 8 | |
Popular customizations:
- Change the ASCII art to a different distro or custom image
- Add/remove information fields
- Change colors
- Add custom ASCII art
1 2 3 4 5 6 7 8 | |
Cowsay: Because Why Not?
Cowsay makes a cow (or other creature) say whatever you want. It's completely useless and absolutely delightful.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Different Cowsay Characters
1 2 3 4 5 6 7 8 9 10 11 | |
Cowsay + Fortune = Magic
1 2 3 4 5 6 7 8 | |
Figlet: Big ASCII Text
Figlet creates large ASCII art text banners. Great for scripts, welcome messages, or just fun.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Figlet Fonts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Combining Tools
1 2 3 4 5 6 7 8 9 10 11 | |
The Infamous sl Command
Here's a Linux easter egg with a lesson: the sl command. Most people have accidentally typed sl when they meant ls. Instead of an error, this package rewards (punishes?) you with a steam locomotive chugging across your terminal!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
The creator made sl because he kept mistyping ls and wanted to train himself to be more careful. The punishment of waiting for a train to cross your screen is surprisingly effective!
More Linux Easter Eggs
The Linux and UNIX world is full of fun surprises hidden by developers with a sense of humor:
apt moo
1 2 | |
Yes Command (Kind of Useless, Entirely Fun)
1 2 3 4 5 6 7 8 | |
Rev (Reverse Text)
1 2 3 4 5 | |
Factor (Prime Factorization)
1 2 3 4 5 | |
Diagram: Linux Easter Eggs Gallery
Linux Easter Eggs Interactive Gallery
Type: infographic
Purpose: Showcase various Linux easter eggs and fun commands with live examples
Bloom Taxonomy: Remember (L1)
Learning Objective: Students will recall various Linux easter eggs and fun commands
Layout: Gallery grid with clickable cards
Cards (each shows command and preview):
- sl - Steam Locomotive
- Preview: ASCII train animation
- Command: sl
-
Fun fact: Created to punish typos
-
cowsay - Talking Cow
- Preview: Cow with speech bubble
- Command: cowsay "message"
-
Variations: -f tux, -f dragon
-
figlet - ASCII Banners
- Preview: Large text example
- Command: figlet "text"
-
Fonts: slant, banner, small
-
fortune - Random Quotes
- Preview: Sample fortune
- Command: fortune
-
Combine: fortune | cowsay
-
cmatrix - Matrix Effect
- Preview: Green falling characters
- Command: cmatrix
-
Install: sudo apt install cmatrix
-
lolcat - Rainbow Text
- Preview: Colorful gradient text
- Command: echo "text" | lolcat
-
Animate: lolcat -a
-
asciiquarium - Fish Tank
- Preview: ASCII fish swimming
- Command: asciiquarium
-
Install: separate package
-
apt moo - Hidden Cow
- Preview: Super cow powers
- Command: apt moo
- Easter egg!
Interactive features: - Click card to see live terminal output - Copy command button - Install instructions for each
Color scheme: Dark terminal theme with syntax highlighting
Implementation: HTML/CSS grid with animated previews
Creating Your Own Welcome Script
Let's combine everything we've learned to create an awesome terminal welcome script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Add to your ~/.bashrc:
1 2 3 4 | |
Key Takeaways
You've learned how to make Linux work for you AND look good doing it! Here's what you can now do:
Automation:
- Schedule tasks with cron using the five-field time format
- Monitor scheduled tasks and check cron logs
- Create professional logging in your scripts
- Mine logs for patterns and create automated alerts
Terminal Fun:
- Use ANSI color codes to add color to terminal output
- Customize your prompt (PS1) with colors and information
- Display system info beautifully with neofetch
- Make animals talk with cowsay
- Create ASCII banners with figlet
- Appreciate the humor of Linux easter eggs like sl
The Bigger Picture:
- Automation saves time and reduces human error
- Good logging makes debugging much easier
- A personalized terminal makes daily work more enjoyable
- The Linux community has a great sense of humor!
Practice Exercises
Exercise 1: Create a Backup Cron Job
Set up a cron job that: - Runs every day at 2:30 AM - Creates a compressed backup of your home directory - Logs the start and end time to /var/log/backup.log - Only keeps backups from the last 7 days
Hint: You'll need to combine cron, tar, find (for cleanup), and logging.
Exercise 2: Build a Custom PS1
Create a PS1 prompt that includes: - Your username in green - The current time in cyan - The current directory in blue - The git branch (if in a git repo) in yellow - A $ on a new line
Hint: Use the PS1 special characters and the parse_git_branch function from the chapter.
Exercise 3: Log Analysis Script
Write a script that analyzes /var/log/syslog and produces a report showing: - Total number of log entries - Breakdown by log level (error, warning, info) - Top 5 most frequent processes writing logs - Any entries containing "fail" or "error"
Hint: Use grep, awk, sort, and uniq.
Exercise 4: Ultimate Welcome Script
Create a welcome script that: - Shows a colorful figlet banner with your name - Displays system stats (CPU temp if on Pi, memory, disk) - Shows the weather for your location (use curl wttr.in) - Ends with a random fortune from a talking Tux
Hint: Combine everything from this chapter!
Remember: A great Linux user doesn't just know the commands—they make the system work for them. Automate the boring stuff, beautify the rest, and never forget that a steam locomotive might be just one typo away! 🚂
References
-
Crontab Guru - Cron Schedule Expression Editor - Interactive tool for creating and understanding cron expressions with examples and explanations.
-
Linux Handbook - Complete Guide to Cron Jobs - Comprehensive tutorial on scheduling tasks, cron syntax, and automation best practices.
-
DigitalOcean - How To Use Cron To Automate Tasks - Step-by-step guide to creating, editing, and troubleshooting cron jobs.
-
journalctl Tutorial - Complete guide to viewing and analyzing systemd logs using journalctl commands.
-
The Art of Command Line - Customization Section - GitHub guide covering terminal customization, ANSI colors, and productivity tools.
-
ANSI Escape Codes Reference - Comprehensive reference for terminal color codes and text formatting.
-
Bash Prompt HOWTO - Detailed guide to customizing PS1 prompts with colors, special characters, and dynamic information.
-
Neofetch GitHub Repository - Official documentation and customization guide for the Neofetch system information tool.
-
Linux Journal - Shell Scripting and Automation - Articles on writing effective automation scripts and monitoring solutions.
-
GeeksforGeeks - Linux Automation - Educational tutorials on cron jobs, task scheduling, and automation techniques.
-
Cowsay and Fortune Documentation - Guide to using cowsay with different characters and combining with fortune for random messages.
-
Figlet Official Documentation - Complete reference for creating ASCII art text banners with different fonts and styles.
-
Red Hat - Log Analysis Best Practices - Professional techniques for mining logs, finding patterns, and troubleshooting issues.
-
TecMint - 20 Useful Terminal Customization Tips - Practical guide to making your terminal beautiful and functional.
-
Linux Foundation - Automation and Scripting Course - Free resources on task automation and monitoring.
-
Opensource.com - Automation Tutorials - Community articles on cron jobs, shell scripts, and Linux automation strategies.
-
lolcat GitHub Repository - Documentation for the rainbow colorization tool with usage examples and installation instructions.
-
Linux Journey - Logging and Monitoring - Interactive lessons on syslog, journald, and log analysis techniques.
-
Bash Scripting Tutorial - Automation - Step-by-step guide to writing scripts for task automation and system monitoring.
-
LinuxConfig - Scheduled Tasks and Automation - Practical examples of automating backups, updates, and system maintenance.