SECTION 05 · 30 MIN READ & EXPLORE
Schedule cleanup and diagnose connectivity
Connect timers to oneshot services and diagnose from listening socket to HTTP response.
Automation (Timers & Cron)
Objective: The app writes to access.log on every request. We need a cleaner script to delete the log file if it gets too big (>10KB). We will use Systemd Timers, which are the modern alternative to Cron.
Step 1: The Cleanup Script
Create /opt/node-app/cleanup.sh:
#!/bin/bash
LOG_FILE="/opt/node-app/access.log"
MAX_SIZE=10240 # 10KB in bytes
if [ -f "$LOG_FILE" ]; then
# Get file size in bytes
FILE_SIZE=$(stat -c%s "$LOG_FILE")
if [ $FILE_SIZE -gt $MAX_SIZE ]; then
echo "$(date): Log file is $FILE_SIZE bytes. Deleting..."
rm "$LOG_FILE"
# Optional: Restart service to recreate file handle if needed
# systemctl try-restart node-app
else
echo "$(date): Log file is clean ($FILE_SIZE bytes)."
fi
fi
Make it executable: chmod +x /opt/node-app/cleanup.sh
Step 2: The Service Unit (The “What”)
Systemd timers trigger a service. Create /etc/systemd/system/log-cleaner.service:
[Unit]
Description=Run the Log Cleanup Script
[Service]
Type=oneshot
ExecStart=/opt/node-app/cleanup.sh
Step 3: The Timer Unit (The “When”)
Create /etc/systemd/system/log-cleaner.timer:
[Unit]
Description=Run Log Cleaner every 1 minute
[Timer]
# Run 1 minute after boot
OnBootSec=1min
# Run every 1 minute thereafter
OnUnitActiveSec=1min
[Install]
WantedBy=timers.target
Activate the Timer:
systemctl daemon-reload
systemctl enable --now log-cleaner.timer
Chapter 4: Networking Cheat Sheet
Objective: Verify your services are listening and accessible.
1. ss (Socket Statistics)
The modern replacement for netstat.
| Command | Usage |
|---|---|
ss -tulpn |
Start here. Shows all listening ports and the process owning them. |
-t(tcp) -u(udp) -l(listening) -p(process) -n(numeric) |
| ss -tulpn | grep 3000 |
2. curl (Client URL)
- The “Swiss Army Knife” for HTTP requests.*
| Command | Usage |
|---|---|
curl localhost:3000 |
Basic GET request. |
curl -v localhost:3000 |
Verbose. Shows Request/Response headers (Crucial for debugging). |
curl -I localhost:3000 |
Head only. Fetches headers without downloading the body. |
curl -o file.html url |
Save output to a file. |
3. Other Essentials
| Tool | Command | Usage |
|---|---|---|
Netcat (nc) |
nc -zv 127.0.0.1 3000 |
Port Check. Checks if a port is open without making a full HTTP request. Useful for verifying firewalls. |
| IP Address | ip a |
Shows IP addresses for all interfaces. |
| Ping | ping -c 4 google.com |
Checks connectivity and latency. |
Lab Verification Task
Use curl to hit your local server 100 times to bloat the log file and test your Systemd Timer:
# Bash loop to fire 100 requests
for i in {1..100}; do curl -s http://localhost:3000 > /dev/null; done
# Check if log size increased
ls -lh /opt/node-app/access.log
Pause and predict
If curl fails, how can you separate a missing listener from an HTTP application error?
Try your own explanation first. These notes stay on this page and are not saved or graded.
Show explanation
Use systemctl and journalctl for process state, ss for listening sockets, nc for a TCP connection, and curl -v for the HTTP exchange.
When finished, stop and disable log-cleaner.timer and node-app on the lab VM, or discard the disposable VM.
Take a moment to explain the idea in your own words.