ps.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/sh
  2. PAGESIZE=`getconf PAGESIZE`;
  3. TOTAL_MEMORY=`cat /proc/meminfo | head -n 1 | awk '{print $2}'`;
  4. # Mimic the output of ps -ax -o pid=,ppid=,pcpu=,pmem=,command=
  5. # Read all numeric subdirectories in /proc
  6. for pid in `cd /proc && ls -d [0-9]*`
  7. do {
  8. if [ -e /proc/$pid/stat ]
  9. then
  10. echo $pid;
  11. # ppid is the word at index 4 in the stat file for the process
  12. awk '{print $4}' /proc/$pid/stat;
  13. # pcpu - calculation will be done later, this is a placeholder value
  14. echo "0.0"
  15. # pmem - ratio of the process's working set size to total memory.
  16. # use the page size to convert to bytes, total memory is in KB
  17. # multiplied by 100 to get percentage, extra 10 to be able to move
  18. # the decimal over by one place
  19. RESIDENT_SET_SIZE=`awk '{print $24}' /proc/$pid/stat`;
  20. PERCENT_MEMORY=$(((1000 * $PAGESIZE * $RESIDENT_SET_SIZE) / ($TOTAL_MEMORY * 1024)));
  21. if [ $PERCENT_MEMORY -lt 10 ]
  22. then
  23. # replace the last character with 0. the last character
  24. echo $PERCENT_MEMORY | sed 's/.$/0.&/'; #pmem
  25. else
  26. # insert . before the last character
  27. echo $PERCENT_MEMORY | sed 's/.$/.&/';
  28. fi
  29. # cmdline
  30. xargs -0 < /proc/$pid/cmdline;
  31. fi
  32. } | tr "\n" "\t"; # Replace newlines with tab so that all info for a process is shown on one line
  33. echo; # But add new lines between processes
  34. done