Kill Previous Instances
killprocessbackground
function kill_prev() {
# $$ contains current pid (grep ignore so it doesn't suicide)
local processes
readarray -t processes < <(pgrep -f "$0" | grep -v "$$")
kill "${processes[@]}" >/dev/null 2>&1
}
# Usage:
# Add this function to your background running script
# It will make sure that only one instance of your script is running at a time
kill_prev
...
System Resource Monitor
filesystem
system_resources () {
echo "CPU Load: $(top -bn1 | grep "Cpu(s)" | awk '{print $2}')%"
echo "Memory Used: $(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}')"
echo "Disk Used: $(df -h / | awk 'NR==2{print $5}')"
echo "Active Users: $(who | wc -l)"
}
system_resources "$@"
# Usage:
chmod a+x system-resource-monitor.sh # First make it executable for all the users
./system-resource-monitor.sh # It will print the following system resources (CPU, RAM, disk, and active users)
...