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

Leader posted Originally published at bashsnippets.xyz 1 min read

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`

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

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.

More Posts

My Nginx Died at 2 AM and Nobody Noticed for 6 Hours. Now I Have a Watchdog Script

BashSnippets - May 21

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

I built a free bash script library because I kept Googling the same 10 scripts over and over

BashSnippets - May 6

I Alias This One-Liner to 'mktoday' and Use It Every Single Week

BashSnippets - May 28

Just completed another large-scale WordPress migration — and the client left this

saqib_devmorph - Apr 7
chevron_left

Commenters (This Week)

33 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!