random bash scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
879 B

  1. #!/usr/bin/env sh
  2. # Get current swap usage for all running processes
  3. # Felix Hauri 2016-08-05
  4. # Rewritted without fork. Inspired by first stuff from
  5. # Erik Ljungstrom 27/05/2011
  6. # Modified by Mikko Rantalainen 2012-08-09
  7. # Pipe the output to "sort -nk3" to get sorted output
  8. # Modified by Marc Methot 2014-09-18
  9. # removed the need for sudo
  10. get_swap() {
  11. OVERALL=0
  12. rifs=$(printf ': \t')
  13. for FILE in /proc/[0-9]*/status; do
  14. SUM=0
  15. while IFS="$rifs" read FIELD VALUE; do
  16. case $FIELD in
  17. Pid) PID=$VALUE ;;
  18. Name) PROGNAME="$VALUE" ;;
  19. VmSwap) SUM=$((SUM = ${VALUE% *})) ;;
  20. esac
  21. done <"$FILE"
  22. [ "$SUM" -gt 0 ] &&
  23. CMDLINE=$(cat /proc/"$PID"/cmdline) &&
  24. printf "PID: %9d swapped: %11d KB (%s): [%s]\n" "$PID" "$SUM" "$PROGNAME" "$CMDLINE"
  25. OVERALL=$((OVERALL + SUM))
  26. done
  27. printf "Total swapped memory: %14u KB\n" "$OVERALL"
  28. }
  29. get_swap | sort -r -k4 -n