DEV Community

Anguishe
Anguishe

Posted on • Originally published at bashsnippets.xyz

I built a couple bash scripts this week after my server crashed Thursday at 11pm

My disk hit 100% on a Thursday night. Just a silent crash and a bunch of errors I found the next day.
I had nothing running in the background watching for problems.

**

  1. Disk space warning** The script I needed before that Thursday happened.
`#!/bin/bash
THRESHOLD=80
USAGE=$(df / | awk 'NR==2{print $5}' | tr -d '%')

if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "⚠ Disk at ${USAGE}% — time to act"
else
  echo "✓ Disk OK: ${USAGE}% used"
fi`
Enter fullscreen mode Exit fullscreen mode

I just happened to be doing some brushing up on crontab a bit lately so I automated the script before I did anything else, so this wouldn't happen again.
Full script with email alerts → bashsnippets.xyz/snippets/disk-space-warning.html

2. Killing Processes
I also found myself continuing to fall into a ps aux | grep ""
loop after cleaning up the disk and new there was a one-word command out there that made it easier, but I couldn't recall...
I had to google it a few times before it stuck with me:

pkill/pgrep <--- turned a 4 command work-flow into a one-word workflow:
My loop was this:

ps aux | grep myapp   # find it
kill 12345            # copy/paste PID
ps aux | grep myapp   # confirm it died
Enter fullscreen mode Exit fullscreen mode

After, it was this:

pgrep -l myapp
pkill -x myapp

Where this went:

So, of course, these and a few more, like error handling, turned into a free, and growing script library at bashsnippets.xyz — copy-paste ready, explained line by line, no signup.

I also started turning each one into a short video on YouTube (@bashsnippets). Hoping to gain some traction in the beginner bash communities!

Hope someone can find some use of this, but What are you automating? Drop it in the comments.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.